Skip to content

Commit 94a33bc

Browse files
committed
Clean code
1 parent 2f60655 commit 94a33bc

File tree

3 files changed

+34
-27
lines changed

3 files changed

+34
-27
lines changed

src/completionProvider.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ export class ReturnHintCompletionProvider extends CompletionProvider implements
183183
const items: CompletionItem[] = [];
184184
const line = doc.lineAt(pos);
185185

186-
if (this.shouldProvideItems(line, pos)) {
186+
if (this.shouldProvideItems(line, pos)) {
187187
const provider = new TypingHintProvider(getDataTypeContainer());
188188
await provider.detectTypingImport(doc.getText());
189189
this.pushHintsToItems(provider.getRemainingHints(), items);
@@ -197,8 +197,7 @@ export class ReturnHintCompletionProvider extends CompletionProvider implements
197197
private shouldProvideItems(line: TextLine, pos: Position): boolean {
198198

199199
if (pos.character > 0 && line.text.substr(pos.character - 2, 2) === "->") {
200-
201-
return new RegExp("\\) *->[: ]*$", "m").test(line.text);
200+
return /\) *->[: ]*$/m.test(line.text);
202201
}
203202
return false;
204203
}

src/typeHintProvider.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ export class TypeHintProvider {
5353
this.tryAddTypingHint(typeName, typeHints);
5454
}
5555

56-
if (
57-
typeHints.length > 0
56+
const typesFound = typeHints.length > 0
5857
|| this.tryAdd(TypeSearch.hintOfSimilarParam(param, documentText), typeHints)
59-
|| this.tryAdd(TypeSearch.classWithSameName(param, documentText), typeHints)
60-
) {
58+
|| this.tryAdd(TypeSearch.classWithSameName(param, documentText), typeHints);
59+
if (typesFound) {
6160
return typeHints;
6261
}
6362

src/typeSearch.ts

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class TypeSearch {
5757
return new VariableSearchResult(typeName, EstimationSource.Value, valueAssignment);
5858
}
5959

60-
match = new RegExp(`^ *([^(\\s#"']+)\\(?`).exec(valueAssignment);
60+
match = /^ *([^(\s#"']+)\(?/.exec(valueAssignment);
6161
if (!match) {
6262
return null;
6363
}
@@ -73,27 +73,12 @@ export class TypeSearch {
7373
return new VariableSearchResult(value, EstimationSource.Value, valueAssignment);
7474
}
7575
} else {
76-
if (value.includes(".")) {
77-
let split = value.split(".");
78-
value = split[split.length - 1];
79-
}
80-
// Find the function definition and check if the return type is hinted
81-
const regExp = new RegExp(`^[ \t]*def ${value}\\([^)]*\\) *-> *([a-zA-Z_][a-zA-Z0-9_.\\[\\]]+)`, "m");
82-
83-
const hintedCallMatch = regExp.exec(src);
84-
85-
if (hintedCallMatch && hintedCallMatch.length === 2) {
86-
return new VariableSearchResult(
87-
hintedCallMatch[1],
88-
EstimationSource.FunctionDefinition,
89-
valueAssignment
90-
);
91-
}
76+
return this.searchForHintedFunctionCall(value, src, valueAssignment);
9277
}
9378
return null;
9479
}
9580

96-
// Searching the import source document is not supported (yet?)
81+
// Searching the import source document is not supported
9782
if (!this.isImported(match[1], src.substr(match.index - match.length))) {
9883
match = this.variableSearchRegExp(match[1]).exec(src);
9984
if (match) {
@@ -106,6 +91,30 @@ export class TypeSearch {
10691
return null;
10792
}
10893

94+
private static searchForHintedFunctionCall(
95+
value: string,
96+
src: string,
97+
valueAssignment: string
98+
): VariableSearchResult | null {
99+
if (value.includes(".")) {
100+
let split = value.split(".");
101+
value = split[split.length - 1];
102+
}
103+
104+
const regExp = new RegExp(`^[ \t]*def ${value}\\([^)]*\\) *-> *([a-zA-Z_][a-zA-Z0-9_.\\[\\]]+)`, "m");
105+
106+
const hintedCallMatch = regExp.exec(src);
107+
108+
if (hintedCallMatch && hintedCallMatch.length === 2) {
109+
return new VariableSearchResult(
110+
hintedCallMatch[1],
111+
EstimationSource.FunctionDefinition,
112+
valueAssignment
113+
);
114+
}
115+
return null;
116+
}
117+
109118
/**
110119
* Detects the type of a value.
111120
*
@@ -171,7 +180,7 @@ export class TypeSearch {
171180
if (searchResult.estimationSource === EstimationSource.ClassDefinition) {
172181
return false;
173182
}
174-
const regExp = new RegExp(" if +[^ ]+ +else( +[^ ]+) *$");
183+
const regExp = / if +[^ ]+ +else( +[^ ]+) *$/;
175184

176185
let ternaryMatch = regExp.exec(searchResult.valueAssignment);
177186
while (ternaryMatch) {
@@ -232,7 +241,7 @@ export class TypeSearch {
232241
exp += `|from +${moduleName} +import +${moduleName} +as +${value}`;
233242
}
234243
exp += ")";
235-
return new RegExp(exp,"m").test(src);
244+
return new RegExp(exp, "m").test(src);
236245
}
237246

238247
private static isProbablyAClass(lineText: string): boolean {

0 commit comments

Comments
 (0)