Skip to content

Commit 9893216

Browse files
committed
Improve parameter extraction
- Avoid providing hints for invalid parameters - Dont consider * as part of the param name
1 parent 7adcbd3 commit 9893216

File tree

1 file changed

+14
-25
lines changed

1 file changed

+14
-25
lines changed

src/completionProvider.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
Range
1212
} from "vscode";
1313
import { TypeHintProvider } from "./typeHintProvider";
14-
import { paramHintTrigger, returnHintTrigger, PythonType, anyClassOrFunctionName } from "./python";
14+
import { paramHintTrigger, returnHintTrigger, PythonType, simpleIdentifier } from "./python";
1515
import { TypeHintSettings } from "./settings";
1616

1717

@@ -63,39 +63,39 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
6363
const precedingText = line.text.substring(0, pos.character - 1).trim();
6464

6565
if (this.shouldProvideItems(precedingText, pos, doc)) {
66-
const param: string = this.findParam(precedingText);
66+
const param = this.getParam(precedingText);
6767
const provider = new TypeHintProvider(doc, this.settings);
6868

69-
if (param.length > 0) {
69+
if (param) {
7070
try {
7171
this.pushEstimationsToItems(await provider.getTypeHints(param), items);
7272
} catch (error) {
73-
}
73+
}
74+
this.pushTypesToItems(provider.getRemainingTypes(), items);
75+
return Promise.resolve(new CompletionList(items, false));
7476
}
75-
this.pushTypesToItems(provider.getRemainingTypes(), items);
76-
return Promise.resolve(new CompletionList(items, false));
7777
}
7878
}
7979
return Promise.resolve(null);
8080
}
8181

8282
/**
83-
* Finds the parameter which is about to be type hinted.
83+
* Returns the parameter which is about to be type hinted.
8484
*
8585
* @param precedingText The text before the active position.
86-
* @returns The parameter.
8786
*/
88-
private findParam(precedingText: string): string {
87+
private getParam(precedingText: string): string | null {
8988
let param = "";
9089

9190
let i = precedingText.length - 1;
9291
let last = precedingText[i];
93-
while (last !== "," && last !== "(" && i >= 0) {
92+
while (last !== "," && last !== "(" && last !== "*" && i >= 0) {
9493
param = precedingText[i] + param;
9594
i--;
9695
last = precedingText[i];
9796
}
98-
return param.trim();
97+
param = param.trim();
98+
return !param || /[),!:?/\\{}.+/=()'"&%¤|<>$^~¨ -]/.test(param) ? null : param;
9999
}
100100

101101
private pushEstimationsToItems(typeHints: string[], items: CompletionItem[]) {
@@ -116,32 +116,21 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
116116

117117
private shouldProvideItems(precedingText: string, activePos: Position, doc: TextDocument): boolean {
118118

119-
if (activePos.character > 0 && !this.isInvalid(precedingText)) {
120-
let provide = new RegExp("^[ \t]*def", "m").test(precedingText);
119+
if (activePos.character > 0 && !/#/.test(precedingText)) {
120+
let provide = new RegExp("^[ \t]*def\\(", "m").test(precedingText);
121121

122122
if (!provide) {
123123
const nLinesToCheck = activePos.line > 4 ? 4 : activePos.line;
124124
const range = new Range(doc.lineAt(activePos.line - nLinesToCheck).range.start, activePos);
125125
provide = new RegExp(
126-
`^[ \t]*def(?![\s\S]+(\\):|-> *${anyClassOrFunctionName}:))`,
126+
`^[ \t]*def(?![\s\S]+(\\):|-> *${simpleIdentifier}:))`,
127127
"m"
128128
).test(doc.getText(range));
129129
}
130130
return provide;
131131
}
132132
return false;
133133
}
134-
135-
/**
136-
* The text is invalid if it is a comment, dict,
137-
* the end of the function definition or preceding a ':' within a string.
138-
*/
139-
private isInvalid(precedingText: string): boolean {
140-
if (precedingText) {
141-
return new RegExp(`#|\\)$|['"][^'",]*$|{ *[a-zA-Z0-9.+*/\\(\\)-]+$`).test(precedingText);
142-
}
143-
return true;
144-
}
145134
}
146135

147136
/**

0 commit comments

Comments
 (0)