forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreferenceProvider.ts
More file actions
60 lines (50 loc) · 2.37 KB
/
referenceProvider.ts
File metadata and controls
60 lines (50 loc) · 2.37 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
'use strict';
import * as vscode from 'vscode';
import * as proxy from './jediProxy';
import * as telemetryContracts from "../common/telemetryContracts";
export class PythonReferenceProvider implements vscode.ReferenceProvider {
private jediProxyHandler: proxy.JediProxyHandler<proxy.IReferenceResult>;
public constructor(context: vscode.ExtensionContext, jediProxy: proxy.JediProxy = null) {
this.jediProxyHandler = new proxy.JediProxyHandler(context, jediProxy);
}
private static parseData(data: proxy.IReferenceResult): vscode.Location[] {
if (data && data.references.length > 0) {
var references = data.references.filter(ref => {
if (!ref || typeof ref.columnIndex !== 'number' || typeof ref.lineIndex !== 'number'
|| typeof ref.fileName !== 'string' || ref.columnIndex === -1 || ref.lineIndex === -1 || ref.fileName.length === 0) {
return false;
}
return true;
}).map(ref => {
var definitionResource = vscode.Uri.file(ref.fileName);
var range = new vscode.Range(ref.lineIndex, ref.columnIndex, ref.lineIndex, ref.columnIndex);
return new vscode.Location(definitionResource, range);
});
return references;
}
return [];
}
public provideReferences(document: vscode.TextDocument, position: vscode.Position, context: vscode.ReferenceContext, token: vscode.CancellationToken): Thenable<vscode.Location[]> {
var filename = document.fileName;
if (document.lineAt(position.line).text.match(/^\s*\/\//)) {
return Promise.resolve(null);
}
if (position.character <= 0) {
return Promise.resolve(null);
}
var range = document.getWordRangeAtPosition(position);
var columnIndex = range.isEmpty ? position.character : range.end.character;
var cmd: proxy.ICommand<proxy.IReferenceResult> = {
command: proxy.CommandType.Usages,
fileName: filename,
columnIndex: columnIndex,
lineIndex: position.line
};
if (document.isDirty) {
cmd.source = document.getText();
}
return this.jediProxyHandler.sendCommand(cmd, token).then(data => {
return PythonReferenceProvider.parseData(data);
});
}
}