This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathgoSignature.ts
executable file
·130 lines (117 loc) · 4.34 KB
/
goSignature.ts
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. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import vscode = require('vscode');
import { SignatureHelpProvider, SignatureHelp, SignatureInformation, ParameterInformation, TextDocument, Position, CancellationToken, WorkspaceConfiguration } from 'vscode';
import { definitionLocation } from './goDeclaration';
import { parameters } from './util';
export class GoSignatureHelpProvider implements SignatureHelpProvider {
private goConfig = null;
constructor(goConfig?: WorkspaceConfiguration) {
this.goConfig = goConfig;
}
public provideSignatureHelp(document: TextDocument, position: Position, token: CancellationToken): Promise<SignatureHelp> {
if (!this.goConfig) {
this.goConfig = vscode.workspace.getConfiguration('go', document.uri);
}
let theCall = this.walkBackwardsToBeginningOfCall(document, position);
if (theCall == null) {
return Promise.resolve(null);
}
let callerPos = this.previousTokenPosition(document, theCall.openParen);
// Temporary fix to fall back to godoc if guru is the set docsTool
let goConfig = this.goConfig;
if (goConfig['docsTool'] === 'guru') {
goConfig = Object.assign({}, goConfig, { 'docsTool': 'godoc' });
}
return definitionLocation(document, callerPos, goConfig, true, token).then(res => {
if (!res) {
// The definition was not found
return null;
}
if (res.line === callerPos.line) {
// This must be a function definition
return null;
}
let declarationText: string = (res.declarationlines || []).join(' ').trim();
if (!declarationText) {
return null;
}
let result = new SignatureHelp();
let sig: string;
let si: SignatureInformation;
if (res.toolUsed === 'godef') {
// declaration is of the form "Add func(a int, b int) int"
let nameEnd = declarationText.indexOf(' ');
let sigStart = nameEnd + 5; // ' func'
let funcName = declarationText.substring(0, nameEnd);
sig = declarationText.substring(sigStart);
si = new SignatureInformation(funcName + sig, res.doc);
} else if (res.toolUsed === 'gogetdoc') {
// declaration is of the form "func Add(a int, b int) int"
declarationText = declarationText.substring(5);
let funcNameStart = declarationText.indexOf(res.name + '('); // Find 'functionname(' to remove anything before it
if (funcNameStart > 0) {
declarationText = declarationText.substring(funcNameStart);
}
si = new SignatureInformation(declarationText, res.doc);
sig = declarationText.substring(res.name.length);
}
si.parameters = parameters(sig).map(paramText =>
new ParameterInformation(paramText)
);
result.signatures = [si];
result.activeSignature = 0;
result.activeParameter = Math.min(theCall.commas.length, si.parameters.length - 1);
return result;
}, () => {
return null;
});
}
private previousTokenPosition(document: TextDocument, position: Position): Position {
while (position.character > 0) {
let word = document.getWordRangeAtPosition(position);
if (word) {
return word.start;
}
position = position.translate(0, -1);
}
return null;
}
private walkBackwardsToBeginningOfCall(document: TextDocument, position: Position): { openParen: Position, commas: Position[] } {
let parenBalance = 0;
let commas = [];
let maxLookupLines = 30;
for (let line = position.line; line >= 0 && maxLookupLines >= 0; line--, maxLookupLines--) {
let currentLine = document.lineAt(line).text;
let characterPosition = document.lineAt(line).text.length - 1;
if (line === position.line) {
characterPosition = position.character;
currentLine = currentLine.substring(0, position.character);
}
for (let char = characterPosition; char >= 0; char--) {
switch (currentLine[char]) {
case '(':
parenBalance--;
if (parenBalance < 0) {
return {
openParen: new Position(line, char),
commas: commas
};
}
break;
case ')':
parenBalance++;
break;
case ',':
if (parenBalance === 0) {
commas.push(new Position(line, char));
}
}
}
}
return null;
}
}