forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockDocumentManager.ts
More file actions
130 lines (122 loc) · 5.67 KB
/
mockDocumentManager.ts
File metadata and controls
130 lines (122 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import * as path from 'path';
import {
DecorationRenderOptions,
Event,
EventEmitter,
Range,
TextDocument,
TextDocumentChangeEvent,
TextDocumentShowOptions,
TextEditor,
TextEditorDecorationType,
TextEditorOptionsChangeEvent,
TextEditorSelectionChangeEvent,
TextEditorViewColumnChangeEvent,
Uri,
ViewColumn,
WorkspaceEdit
} from 'vscode';
import { IDocumentManager } from '../../client/common/application/types';
import { EXTENSION_ROOT_DIR } from '../../client/constants';
import { MockDocument } from './mockDocument';
import { MockEditor } from './mockTextEditor';
// tslint:disable:no-any no-http-string no-multiline-string max-func-body-length
export class MockDocumentManager implements IDocumentManager {
public textDocuments: TextDocument[] = [];
public activeTextEditor: TextEditor | undefined;
public visibleTextEditors: TextEditor[] = [];
private didChangeEmitter = new EventEmitter<TextEditor>();
private didOpenEmitter = new EventEmitter<TextDocument>();
private didChangeVisibleEmitter = new EventEmitter<TextEditor[]>();
private didChangeTextEditorSelectionEmitter = new EventEmitter<TextEditorSelectionChangeEvent>();
private didChangeTextEditorOptionsEmitter = new EventEmitter<TextEditorOptionsChangeEvent>();
private didChangeTextEditorViewColumnEmitter = new EventEmitter<TextEditorViewColumnChangeEvent>();
private didCloseEmitter = new EventEmitter<TextDocument>();
private didSaveEmitter = new EventEmitter<TextDocument>();
private didChangeTextDocumentEmitter = new EventEmitter<TextDocumentChangeEvent>();
public get onDidChangeActiveTextEditor(): Event<TextEditor> {
return this.didChangeEmitter.event;
}
public get onDidChangeTextDocument(): Event<TextDocumentChangeEvent> {
return this.didChangeTextDocumentEmitter.event;
}
public get onDidOpenTextDocument(): Event<TextDocument> {
return this.didOpenEmitter.event;
}
public get onDidChangeVisibleTextEditors(): Event<TextEditor[]> {
return this.didChangeVisibleEmitter.event;
}
public get onDidChangeTextEditorSelection(): Event<TextEditorSelectionChangeEvent> {
return this.didChangeTextEditorSelectionEmitter.event;
}
public get onDidChangeTextEditorOptions(): Event<TextEditorOptionsChangeEvent> {
return this.didChangeTextEditorOptionsEmitter.event;
}
public get onDidChangeTextEditorViewColumn(): Event<TextEditorViewColumnChangeEvent> {
return this.didChangeTextEditorViewColumnEmitter.event;
}
public get onDidCloseTextDocument(): Event<TextDocument> {
return this.didCloseEmitter.event;
}
public get onDidSaveTextDocument(): Event<TextDocument> {
return this.didSaveEmitter.event;
}
public showTextDocument(_document: TextDocument, _column?: ViewColumn, _preserveFocus?: boolean): Thenable<TextEditor>;
public showTextDocument(_document: TextDocument | Uri, _options?: TextDocumentShowOptions): Thenable<TextEditor>;
public showTextDocument(_document: any, _column?: any, _preserveFocus?: any): Thenable<TextEditor> {
const mockEditor = new MockEditor(this, this.lastDocument as MockDocument);
this.activeTextEditor = mockEditor;
return Promise.resolve(mockEditor);
}
public openTextDocument(_fileName: string | Uri): Thenable<TextDocument>;
public openTextDocument(_options?: { language?: string; content?: string }): Thenable<TextDocument>;
public openTextDocument(_options?: any): Thenable<TextDocument> {
return Promise.resolve(this.lastDocument);
}
public applyEdit(_edit: WorkspaceEdit): Thenable<boolean> {
throw new Error('Method not implemented.');
}
public addDocument(code: string, file: string) {
const mockDoc = new MockDocument(code, file, this.saveDocument);
this.textDocuments.push(mockDoc);
}
public changeDocument(file: string, changes: { range: Range; newText: string }[]) {
const doc = this.textDocuments.find(d => d.fileName === file) as MockDocument;
if (doc) {
const contentChanges = changes.map(c => {
const startOffset = doc.offsetAt(c.range.start);
const endOffset = doc.offsetAt(c.range.end);
return {
range: c.range,
rangeOffset: startOffset,
rangeLength: endOffset - startOffset,
text: c.newText
};
});
const ev: TextDocumentChangeEvent = {
document: doc,
contentChanges
};
// Changes are applied to the doc before it's sent.
ev.contentChanges.forEach(doc.edit.bind(doc));
this.didChangeTextDocumentEmitter.fire(ev);
}
}
public createTextEditorDecorationType(_options: DecorationRenderOptions): TextEditorDecorationType {
throw new Error('Method not implemented');
}
private get lastDocument(): TextDocument {
if (this.textDocuments.length > 0) {
return this.textDocuments[this.textDocuments.length - 1];
}
throw new Error('No documents in MockDocumentManager');
}
private saveDocument = (doc: TextDocument): Promise<boolean> => {
// Create a new document with the contents of the doc passed in
this.addDocument(doc.getText(), path.join(EXTENSION_ROOT_DIR, 'baz.py'));
return Promise.resolve(true);
}
}