forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjectDefinitionProvider.ts
More file actions
87 lines (76 loc) · 3.07 KB
/
objectDefinitionProvider.ts
File metadata and controls
87 lines (76 loc) · 3.07 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
'use strict';
import * as vscode from 'vscode';
import { JediFactory } from '../languageServices/jediProxyFactory';
import { captureTelemetry } from '../telemetry';
import { EventName } from '../telemetry/constants';
import * as defProvider from './definitionProvider';
export class PythonObjectDefinitionProvider {
private readonly _defProvider: defProvider.PythonDefinitionProvider;
public constructor(jediFactory: JediFactory) {
this._defProvider = new defProvider.PythonDefinitionProvider(jediFactory);
}
@captureTelemetry(EventName.GO_TO_OBJECT_DEFINITION)
public async goToObjectDefinition() {
const pathDef = await this.getObjectDefinition();
if (typeof pathDef !== 'string' || pathDef.length === 0) {
return;
}
const parts = pathDef.split('.');
let source = '';
let startColumn = 0;
if (parts.length === 1) {
source = `import ${parts[0]}`;
startColumn = 'import '.length;
} else {
const mod = parts.shift();
source = `from ${mod} import ${parts.join('.')}`;
startColumn = `from ${mod} import `.length;
}
const range = new vscode.Range(0, startColumn, 0, source.length - 1);
const doc = <vscode.TextDocument>(<any>{
fileName: 'test.py',
lineAt: (_line: number) => {
return { text: source };
},
getWordRangeAtPosition: (_position: vscode.Position) => range,
isDirty: true,
getText: () => source,
});
const tokenSource = new vscode.CancellationTokenSource();
const defs = await this._defProvider.provideDefinition(doc, range.start, tokenSource.token);
if (defs === null) {
await vscode.window.showInformationMessage(`Definition not found for '${pathDef}'`);
return;
}
let uri: vscode.Uri | undefined;
let lineNumber: number;
if (Array.isArray(defs) && defs.length > 0) {
uri = defs[0].uri;
lineNumber = defs[0].range.start.line;
}
if (defs && !Array.isArray(defs) && defs.uri) {
uri = defs.uri;
lineNumber = defs.range.start.line;
}
if (uri) {
const openedDoc = await vscode.workspace.openTextDocument(uri);
await vscode.window.showTextDocument(openedDoc);
await vscode.commands.executeCommand('revealLine', { lineNumber: lineNumber!, at: 'top' });
} else {
await vscode.window.showInformationMessage(`Definition not found for '${pathDef}'`);
}
}
private intputValidation(value: string): string | undefined | null {
if (typeof value !== 'string') {
return '';
}
value = value.trim();
if (value.length === 0) {
return '';
}
return null;
}
private async getObjectDefinition(): Promise<string | undefined> {
return vscode.window.showInputBox({ prompt: 'Enter Object Path', validateInput: this.intputValidation });
}
}