Skip to content

Commit 58a2234

Browse files
committed
Minor fixes
1 parent 8523fb1 commit 58a2234

File tree

6 files changed

+80
-68
lines changed

6 files changed

+80
-68
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ The extension is not published yet.
1515

1616
## Known Issues
1717

18-
* The difference between classes and function calls when detecting types is determined by the first letter being upper case (unless the function is declared in the currently edited document).
18+
* The difference between classes and function calls when detecting types is determined by the first letter being upper case (unless the class or function is declared in the currently edited document).
1919

2020
-------------------------------------------------------------------------------------------

src/codeSearch.ts

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
import {
22
TextLine,
3-
Position
3+
Position,
4+
TextDocument
45
} from "vscode";
56

67
import { anyTypeName, Type } from "./syntax";
78

8-
/**
9-
* Finds the param which is about to be type hinted.
10-
*
11-
* @param line The active line.
12-
* @param pos The active position.
13-
*/
14-
export function findParam(line: TextLine, pos: Position): string | null {
15-
let param = null;
16-
let split = line.text.substr(0, pos.character).split(new RegExp("[,(]"));
17-
if (split.length > 1) {
18-
param = split[split.length - 1].trim();
19-
param = param.substr(0, param.length - 1);
20-
}
21-
return param;
22-
}
23-
249
/**
2510
* Detects the type of an initialized variable.
2611
*
@@ -46,7 +31,7 @@ export function detectBasicType(src: string, srcIsLineOfCode = true): string | n
4631
* @param documentText The source code of the text document.
4732
* @returns The type or null if not found.
4833
*/
49-
export function detectNotBasicType(lineText: string, documentText: string): string | null {
34+
export function detectNonBasicType(lineText: string, documentText: string): string | null {
5035
let regExp = new RegExp("= *(" + anyTypeName + ")\\(?");
5136
const match = regExp.exec(lineText);
5237

@@ -56,6 +41,10 @@ export function detectNotBasicType(lineText: string, documentText: string): stri
5641

5742
if (match[0].endsWith("(")) {
5843

44+
if (isClass(match[1], documentText)) {
45+
return match[1];
46+
}
47+
5948
if (isProbablyAClass(match[1])) {
6049
regExp = new RegExp(`^[ \t]*def ${match[1]}\\(`, "m" );
6150
if (!regExp.test(documentText)) {
@@ -75,20 +64,15 @@ export function detectNotBasicType(lineText: string, documentText: string): stri
7564
}
7665
return null;
7766
}
78-
if (
79-
isImported(
80-
match[1],
81-
documentText.substr(match.index - match.length)
82-
)
83-
) {
67+
if (importFound(match[1], documentText.substr(match.index - match.length))) {
8468
// Searching the import source document is not supported (yet?)
8569
return null;
8670
}
8771

8872
regExp = new RegExp(`^[ \t]*${match[1]} *=.*`, "m");
89-
let objInitializationMatch = regExp.exec(documentText);
90-
if (objInitializationMatch) {
91-
return detectBasicType(objInitializationMatch[0]);
73+
let varInitializationMatch = regExp.exec(documentText);
74+
if (varInitializationMatch) {
75+
return detectBasicType(varInitializationMatch[0]);
9276
}
9377

9478
return null;
@@ -125,16 +109,22 @@ export function invalidTernaryOperator(typeName: string, lineSrc: string) {
125109
return false;
126110
}
127111

128-
function isImported(text: string, documentText: string): boolean {
112+
function isClass(object: string, documentText: string) {
113+
return new RegExp(
114+
`^ *class +${object}`,
115+
"m"
116+
).test(documentText);
117+
}
118+
119+
function importFound(object: string, documentText: string): boolean {
129120
return new RegExp(
130-
`^ *(import +${text}|from +[a-zA-Z_][a-zA-Z0-9_-]* +import +${text}`
131-
+ `|import +${anyTypeName} +as +${text})`,
121+
`^[ \t]*(import +${object}|from +[a-zA-Z_][a-zA-Z0-9_-]* +import +${object}|import +${anyTypeName} +as +${object})`,
132122
"m"
133123
).test(documentText);
134124
}
135125

136-
function isProbablyAClass(text: string): boolean {
137-
return new RegExp(`^([a-zA-Z0-9_]+\\.)*[A-Z]`, "m").test(text);
126+
function isProbablyAClass(lineText: string): boolean {
127+
return new RegExp(`^([a-zA-Z0-9_]+\\.)*[A-Z]`, "m").test(lineText);
138128
}
139129

140130
function isType(text: string): boolean {
@@ -164,9 +154,11 @@ function getTypeRegEx(typeName: string, prefix: string): RegExp {
164154
case Type.Tuple:
165155
return new RegExp(`${prefix}(\\(([^'\",)]+,|\"[^\"]*\"(?= *,)|'[^']*'(?= *,))|tuple\\()`, "m");
166156
case Type.Complex:
167-
return new RegExp(`${prefix}(\\(complex\\(|[0-9][0-9+-/*.]*[jJ])`, "m");
157+
return new RegExp(`${prefix}(\\(complex\\(|[0-9][0-9+-/*.]*[jJ])`, "m");
158+
case Type.Object:
159+
return new RegExp(`${prefix}object\\(`, "m");
168160
default:
169-
return new RegExp(`${prefix}object\\(`, "m");
161+
return new RegExp(`^.*$`, "m");
170162
}
171163
}
172164

src/completionProvider.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
import {
22
CancellationToken,
33
CompletionContext,
4+
CompletionList,
45
CompletionItem,
56
CompletionItemKind,
67
CompletionItemProvider,
78
Position,
9+
TextLine,
810
TextDocument
911
} from "vscode";
10-
11-
import { findParam } from "./codeSearch";
12-
import { TypeResolver } from "./typeResolution";
12+
import { TypeResolver } from "./typeResolver";
1313
import { typeHintCharacter, Type } from "./syntax";
1414

1515
/**
1616
* Provides one or more type hint {@link CompletionItem}.
1717
*/
1818
export class HintCompletionProvider implements CompletionItemProvider {
19-
public i = 1;
19+
2020
public provideCompletionItems(
2121
doc: TextDocument,
2222
pos: Position,
2323
token: CancellationToken,
2424
context: CompletionContext
25-
): Thenable<CompletionItem[]> | null {
25+
): Thenable<CompletionList> | null {
2626
if (context.triggerCharacter !== typeHintCharacter) {
2727
return null;
2828
}
2929
const items: CompletionItem[] = [];
3030
const line = doc.lineAt(pos);
31-
const param = findParam(line, pos);
31+
const param = this.findParam(line, pos);
3232

3333
if (param && param.length > 0) {
34-
let hint = new TypeResolver().FindTypeInDocument(doc, param);
34+
let hint = new TypeResolver().EstimateType(doc, param);
3535

3636
if (hint) {
3737
items.push(new CompletionItem(" " + hint, CompletionItemKind.TypeParameter));
@@ -41,7 +41,22 @@ export class HintCompletionProvider implements CompletionItemProvider {
4141
}
4242
}
4343
}
44-
this.i++;
45-
return Promise.resolve(items);
44+
return Promise.resolve(new CompletionList(items, false));
45+
}
46+
47+
/**
48+
* Finds the parameter which is about to be type hinted.
49+
*
50+
* @param line The active line.
51+
* @param pos The active position.
52+
*/
53+
private findParam(line: TextLine, pos: Position): string | null {
54+
let param = null;
55+
let split = line.text.substr(0, pos.character).split(new RegExp("[,(]"));
56+
if (split.length > 1) {
57+
param = split[split.length - 1].trim();
58+
param = param.substr(0, param.length - 1);
59+
}
60+
return param;
4661
}
4762
}

src/test/suite/codeSearch/detectNotBasicType.test.ts renamed to src/test/suite/codeSearch/detectNonBasicType.test.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
11
import * as assert from 'assert';
22
import { getErrorMessage } from "../common";
3-
import { detectNotBasicType } from "../../../codeSearch";
3+
import { detectNonBasicType } from "../../../codeSearch";
44

5-
suite('detectNotBasicType', function() {
5+
suite('detectNonBasicType', function() {
66

7-
test("detects variable types if initialized within the document", () => {
7+
test("detects class if defined in the document", () => {
8+
const expected = "test_class";
9+
const line = "var = test_class()";
10+
let src = `class ${expected}:\n\tpass\n${line}`;
11+
let actual = detectNonBasicType(line, src);
12+
13+
assert.equal(actual, expected, getErrorMessage(src, expected, actual));
14+
});
15+
16+
test("detects variable types if initialized in the document", () => {
817
const expected = "int";
918
const line = "var = x";
1019

1120
// Variable with the same name initialized above
1221
let src = `${line}\n\ndef main():\n\tx = 5`;
13-
let actual = detectNotBasicType(line, src);
22+
let actual = detectNonBasicType(line, src);
1423
assert.equal(actual, expected, getErrorMessage(src, expected, actual));
1524

1625
// Variable with the same name initialized below
1726
src = `x = 5\n${line}`;
18-
actual = detectNotBasicType(line, src);
27+
actual = detectNonBasicType(line, src);
1928
assert.equal(actual, expected, getErrorMessage(src, expected, actual));
2029
});
2130

2231
test("considers value to be an object initializtion if title case", () => {
2332
let expected = "TestClass";
2433
let src = `var = ${expected}(x)`;
25-
let actual = detectNotBasicType(src, src);
34+
let actual = detectNonBasicType(src, src);
2635
assert.equal(actual, expected, getErrorMessage(src, expected, actual));
2736

2837
expected = "test.test.TestClass";
2938
src = `var = ${expected}(x)`;
30-
actual = detectNotBasicType(src, src);
39+
actual = detectNonBasicType(src, src);
3140
assert.equal(actual, expected, getErrorMessage(src, expected, actual));
3241
});
3342

34-
test("returns null for title case functions (if the function is defined within the document)", () => {
43+
test("returns null for title case functions (if the function is defined in the document)", () => {
3544
const expected = null;
3645
const line = "var = Func(x)";
3746

3847
let src = `def Func(x):\n\tpass\n\n${line}`;
39-
let actual = detectNotBasicType(line, src);
48+
let actual = detectNonBasicType(line, src);
4049
assert.equal(actual, expected);
4150
});
4251

@@ -45,22 +54,22 @@ suite('detectNotBasicType', function() {
4554
const expected = "int";
4655
let line = "var = test()";
4756
let src = "def test() -> int:\n\treturn 1\n\nvar = test()";
48-
let actual = detectNotBasicType(line, src);
57+
let actual = detectNonBasicType(line, src);
4958
assert.equal(actual, expected);
5059
});
5160

5261
test("doesn't consider function calls to be variables", () => {
5362
const expected = null;
5463
let src = `obj = call()`;
55-
let actual = detectNotBasicType(src, src);
64+
let actual = detectNonBasicType(src, src);
5665
assert.equal(actual, expected);
5766
});
5867

5968
test("ignores single-line comments", () => {
6069
const expected = null;
6170
let line = "var = obj";
6271
let src = `# obj = 5\n${line}`;
63-
let actual = detectNotBasicType(line, src);
72+
let actual = detectNonBasicType(line, src);
6473
assert.equal(actual, expected);
6574
});
6675

src/test/suite/codeSearch/findParam.test.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/typeResolution.ts renamed to src/typeResolver.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ import * as search from "./codeSearch";
44
export class TypeResolver {
55

66
/**
7-
* Finds the type of a parameter.
7+
* Estimates the type of a parameter.
88
* @param doc The document to search.
99
* @param param The parameter name.
1010
*/
11-
public FindTypeInDocument(doc: TextDocument, param: string): string | null {
11+
public EstimateType(doc: TextDocument, param: string): string | null {
1212
const documentText = doc.getText();
1313
let type: string | null = "";
14-
let m = new RegExp("^[ \t]*" + param + " *=", "m").exec(documentText);
15-
if (m && m[0]) {
16-
let line = doc.lineAt(doc.positionAt(m.index + m[0].length - 1));
14+
const match = new RegExp(`^[ \t]*${param} *=.`, "m").exec(documentText);
15+
16+
if (match && match[0]) {
17+
const valueStartPosition = doc.positionAt(match.index + match[0].length);
18+
const line = doc.lineAt(valueStartPosition);
1719

1820
type = search.detectBasicType(line.text);
1921

2022
if (type === null) {
21-
type = search.detectNotBasicType(line.text, documentText);
23+
type = search.detectNonBasicType(line.text, documentText);
2224
}
2325

2426
if (type !== null) {

0 commit comments

Comments
 (0)