Skip to content

Commit e6dee37

Browse files
committed
Cleanup and optimize
1 parent 181f0d1 commit e6dee37

File tree

4 files changed

+33
-49
lines changed

4 files changed

+33
-49
lines changed

src/settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class TypeHintSettings {
3535
if (wsEnable !== undefined) {
3636
this._workspaceSearchEnabled = wsEnable;
3737
}
38-
if (searchLimit || searchLimit === 0) {
38+
if (searchLimit !== undefined) {
3939
this._workspaceSearchLimit = Number.isInteger(searchLimit) ? searchLimit : Math.round(searchLimit);
4040
}
4141
}

src/typeHintProvider.ts

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TextDocument } from "vscode";
2-
import { PythonType as PythonType, DataTypeContainer, getDataTypeContainer } from "./python";
2+
import { PythonType as PythonType, DataTypeContainer } from "./python";
33
import { TypeSearch, VariableSearchResult } from "./typeSearch";
44
import { TypingHintProvider } from "./typingHintProvider";
55
import { TypeHintSettings } from "./settings";
@@ -12,27 +12,20 @@ export class TypeHintProvider {
1212
private _typingHintProvider: TypingHintProvider;
1313

1414
private likelyTypes: PythonType[] = [
15-
PythonType.String,
1615
PythonType.List,
17-
PythonType.Dict,
16+
PythonType.Dict,
17+
PythonType.String,
1818
PythonType.Bool,
1919
PythonType.Int,
2020
PythonType.Tuple,
2121
PythonType.Float
2222
];
2323
private _providedTypeHints: string[] = [];
24-
private _typeContainer: DataTypeContainer;
25-
private _typingImported = false;
2624

2725
constructor(typeContainer: DataTypeContainer) {
28-
this._typeContainer = typeContainer;
2926
this._typingHintProvider = new TypingHintProvider(typeContainer);
3027
}
3128

32-
public get typingImported() {
33-
return this._typingImported;
34-
}
35-
3629
/**
3730
* Estimates a parameter's type and returns type hints for it.
3831
* The returned hints are ordered with the most likely type being first.
@@ -43,14 +36,13 @@ export class TypeHintProvider {
4336
public async estimateTypeHints(param: string, documentText: string): Promise<string[]> {
4437
const typeHints: string[] = [];
4538

46-
const typingSearch = this._typingHintProvider.detectTypingImport(documentText);
4739
const variableSearch = TypeSearch.variableWithSameName(param, documentText);
4840

41+
const isTypingImported = this._typingHintProvider.detectTypingImport(documentText);
4942
let typeName = this.getTypeParamEndsWith(param, "_");
50-
this._typingImported = await typingSearch;
5143
if (typeName) {
5244
this.add(typeName, typeHints);
53-
this.tryAddTypingHint(typeName, typeHints);
45+
this.tryAddTypingHint(typeName, typeHints, isTypingImported);
5446
}
5547

5648
const typesFound = typeHints.length > 0
@@ -64,15 +56,15 @@ export class TypeHintProvider {
6456

6557
if (searchResult !== null && !TypeSearch.invalidTernaryOperator(searchResult)) {
6658
this.add(searchResult.typeName, typeHints);
67-
this.tryAddTypingHints(searchResult, typeHints);
68-
this.tryAdd(this.typeGuessFor(param, typeHints), typeHints);
59+
this.tryAddTypingHints(searchResult, typeHints, isTypingImported);
60+
this.tryAdd(this.typeGuessFor(param), typeHints);
6961
} else {
7062
typeName = this.getTypeParamEndsWith(param, "");
7163
if (typeName) {
7264
this.add(typeName, typeHints);
73-
this.tryAddTypingHint(typeName, typeHints);
65+
this.tryAddTypingHint(typeName, typeHints, isTypingImported);
7466
} else {
75-
this.tryAdd(this.typeGuessFor(param, typeHints), typeHints);
67+
this.tryAdd(this.typeGuessFor(param), typeHints);
7668
}
7769
}
7870
return typeHints;
@@ -82,15 +74,7 @@ export class TypeHintProvider {
8274
* Returns hints for types that have not been provided yet.
8375
*/
8476
public remainingTypeHints(): PythonType[] {
85-
const result: PythonType[] = [];
86-
87-
for (const type of Object.values(this._typeContainer)) {
88-
if (this.hintNotProvided(type.name)) {
89-
result.push(type.name);
90-
}
91-
}
92-
93-
return result;
77+
return Object.values(PythonType).filter(type => this.hintNotProvided(type));
9478
}
9579

9680
/**
@@ -107,7 +91,7 @@ export class TypeHintProvider {
10791
/**
10892
* If the param ends with a type name, the type is returned.
10993
* @param param The parameter name.
110-
* @param prefix A prefix before the typename. For example, a param named x{prefix}list will return 'list'.
94+
* @param prefix A prefix before the typename. For example, a param named {prefix}list will return 'list'.
11195
*/
11296
private getTypeParamEndsWith(param: string, prefix: string): PythonType | null {
11397
const paramUpper = param.toUpperCase();
@@ -120,7 +104,7 @@ export class TypeHintProvider {
120104
return null;
121105
}
122106

123-
private typeGuessFor(param: string, typeHints: string[]): string | null {
107+
private typeGuessFor(param: string): string | null {
124108
const typeGuesses: { [key: string]: string } = {
125109
"string": PythonType.String,
126110
"text": PythonType.String,
@@ -133,7 +117,6 @@ export class TypeHintProvider {
133117
"num": PythonType.Int
134118
};
135119
if (param in typeGuesses) {
136-
this.add(typeGuesses[param], typeHints);
137120
return typeGuesses[param];
138121
}
139122
return null;
@@ -156,8 +139,8 @@ export class TypeHintProvider {
156139
return false;
157140
}
158141

159-
private tryAddTypingHints(searchResult: VariableSearchResult | null, typeHints: string[]) {
160-
if (this._typingImported) {
142+
private tryAddTypingHints(searchResult: VariableSearchResult | null, typeHints: string[], typingImported: boolean) {
143+
if (typingImported) {
161144
const hints: string[] | null = this._typingHintProvider.getHints(searchResult);
162145
if (hints) {
163146
for (const hint of hints) {
@@ -167,8 +150,8 @@ export class TypeHintProvider {
167150
}
168151
}
169152

170-
private tryAddTypingHint(typeName: string, typeHints: string[]) {
171-
if (this._typingImported) {
153+
private tryAddTypingHint(typeName: string, typeHints: string[], typingImported: boolean) {
154+
if (typingImported) {
172155
const typingHint = this._typingHintProvider.getHint(typeName);
173156
if (typingHint) {
174157
this.add(typingHint, typeHints);

src/typeSearch.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,24 @@ export class TypeSearch {
4646
* @param src The source code to search.
4747
*/
4848
public static async variableWithSameName(param: string, src: string): Promise<VariableSearchResult | null> {
49-
let match = this.variableSearchRegExp(param).exec(src);
50-
if (!match) {
49+
let variableMatch = this.variableSearchRegExp(param).exec(src);
50+
if (!variableMatch) {
5151
return null;
5252
}
53-
const valueAssignment = match[1];
53+
const valueAssignment = variableMatch[1];
5454

5555
let typeName = this.detectType(valueAssignment);
5656
if (typeName) {
5757
return new VariableSearchResult(typeName, EstimationSource.Value, valueAssignment);
5858
}
5959

60-
match = /^ *([^(\s#"']+)\(?/.exec(valueAssignment);
61-
if (!match) {
60+
variableMatch = /^ *([^(\s#"']+)\(?/.exec(valueAssignment);
61+
if (!variableMatch) {
6262
return null;
6363
}
6464

65-
if (match[0].endsWith("(")) {
66-
let value = match[1];
65+
if (variableMatch[0].endsWith("(")) {
66+
let value = variableMatch[1];
6767
if (this.classWithSameName(value, src)) {
6868
return new VariableSearchResult(value, EstimationSource.ClassDefinition, valueAssignment);
6969
}
@@ -79,10 +79,10 @@ export class TypeSearch {
7979
}
8080

8181
// Searching the import source document is not supported
82-
if (!this.isImported(match[1], src.substr(match.index - match.length))) {
83-
match = this.variableSearchRegExp(match[1]).exec(src);
84-
if (match) {
85-
const otherType = this.detectType(match[1]);
82+
if (!this.isImported(variableMatch[1], src.substr(variableMatch.index - variableMatch.length))) {
83+
variableMatch = this.variableSearchRegExp(variableMatch[1]).exec(src);
84+
if (variableMatch) {
85+
const otherType = this.detectType(variableMatch[1]);
8686
return otherType
8787
? new VariableSearchResult(otherType, EstimationSource.ValueOfOtherVariable, valueAssignment)
8888
: null;

src/typingHintProvider.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { VariableSearchResult, TypeSearch } from "./typeSearch";
77
*/
88
export class TypingHintProvider {
99

10+
private collectionTypes: DataType[];
1011
private typeContainer: DataTypeContainer;
1112
private typingImportDetected: boolean = false;
1213
private fromTypingImport: boolean = false;
@@ -20,6 +21,7 @@ export class TypingHintProvider {
2021
*/
2122
constructor(typeContainer: DataTypeContainer) {
2223
this.typeContainer = typeContainer;
24+
this.collectionTypes = Object.values(typeContainer).filter(t => t.category === TypeCategory.Collection);
2325
}
2426

2527
/**
@@ -28,8 +30,7 @@ export class TypingHintProvider {
2830
* @param docText The document text to search.
2931
* @returns True if typing is imported.
3032
*/
31-
public async detectTypingImport(docText: string): Promise<boolean> {
32-
33+
public detectTypingImport(docText: string): boolean {
3334
if (/^[ \t]*from typing import +([A-Z][a-zA-Z0-9 ,]+)/m.exec(docText)) {
3435
this.fromTypingImport = true;
3536
this.typingImportDetected = true;
@@ -127,7 +128,7 @@ export class TypingHintProvider {
127128
return this.hintsForAllCollectionTypes();
128129
}
129130
const result: string[] = [];
130-
for (const type of Object.values(this.typeContainer).filter(t => t.category === TypeCategory.Collection)) {
131+
for (const type of this.collectionTypes) {
131132
if (!this.providedTypes.includes(type)) {
132133
result.push(this.toTypingString(type.name, this.fromTypingImport));
133134
}
@@ -138,7 +139,7 @@ export class TypingHintProvider {
138139
private hintsForAllCollectionTypes(): string[] {
139140
const firstHalf: string[] = [];
140141
const secondHalf: string[] = [];
141-
for (const type of Object.values(this.typeContainer).filter(t => t.category === TypeCategory.Collection)) {
142+
for (const type of this.collectionTypes) {
142143
if (!this.providedTypes.includes(type)) {
143144
const withoutPrefix = this.fromTypingImport || !this.typingImportDetected;
144145
firstHalf.push(this.toTypingString(type.name, withoutPrefix));

0 commit comments

Comments
 (0)