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
61 lines (53 loc) · 2.45 KB
/
referenceProvider.ts
File metadata and controls
61 lines (53 loc) · 2.45 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
'use strict';
import * as vscode from 'vscode';
import { JediFactory } from '../languageServices/jediProxyFactory';
import { captureTelemetry } from '../telemetry';
import { EventName } from '../telemetry/constants';
import * as proxy from './jediProxy';
export class PythonReferenceProvider implements vscode.ReferenceProvider {
public constructor(private jediFactory: JediFactory) { }
private static parseData(data: proxy.IReferenceResult): vscode.Location[] {
if (data && data.references.length > 0) {
// tslint:disable-next-line:no-unnecessary-local-variable
const 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 => {
const definitionResource = vscode.Uri.file(ref.fileName);
const range = new vscode.Range(ref.lineIndex, ref.columnIndex, ref.lineIndex, ref.columnIndex);
return new vscode.Location(definitionResource, range);
});
return references;
}
return [];
}
@captureTelemetry(EventName.REFERENCE)
public async provideReferences(document: vscode.TextDocument, position: vscode.Position, _context: vscode.ReferenceContext, token: vscode.CancellationToken): Promise<vscode.Location[] | undefined> {
const filename = document.fileName;
if (document.lineAt(position.line).text.match(/^\s*\/\//)) {
return;
}
if (position.character <= 0) {
return;
}
const range = document.getWordRangeAtPosition(position);
if (!range) {
return;
}
const columnIndex = range.isEmpty ? position.character : range.end.character;
const cmd: proxy.ICommand = {
command: proxy.CommandType.Usages,
fileName: filename,
columnIndex: columnIndex,
lineIndex: position.line
};
if (document.isDirty) {
cmd.source = document.getText();
}
const data = await this.jediFactory.getJediProxyHandler<proxy.IReferenceResult>(document.uri).sendCommand(cmd, token);
return data ? PythonReferenceProvider.parseData(data) : undefined;
}
}