Skip to content

Commit d32a278

Browse files
committed
Add user setting and prepare release
1 parent b6e8aab commit d32a278

File tree

9 files changed

+87
-19
lines changed

9 files changed

+87
-19
lines changed

CHANGELOG.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# Change Log
22

3-
All notable changes to the "vscode-python-autohint" extension will be documented in this file.
3+
## [1.0.0]
44

5-
## [0.5.0]
6-
7-
- Initial release
5+
* Initial release

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# vscode-python-autohint README
1+
# Python Type Hint
22

33
Provides type hint auto-completion for Python.
44

@@ -10,12 +10,16 @@ Provides type hint auto-completion for Python.
1010

1111
* Searches Python files in the workspace for type estimation purposes.
1212

13+
* Can provide completion items for the typing module if it is imported.
14+
1315
## Installation
1416

1517
Get this extension from the [Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=).
1618

1719
## Known Issues
1820

19-
* 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 defined in the currently edited document).
21+
* The difference between function and class constructor calls when detecting types is determined by the first letter being upper case (unless the class or function is defined in the currently edited document).
22+
23+
* Up to 20 files within in the workspace are searched at a time. The limit is by design to keep the extension light weight, but there's probably a better solution.
2024

2125
-------------------------------------------------------------------------------------------

images/icon.png

-2.6 KB
Loading

package.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,21 @@
3939
".py"
4040
]
4141
}
42-
]
42+
],
43+
"configuration": {
44+
"title": "Python Type Hint",
45+
"properties": {
46+
"pyTypehint.search.limit": {
47+
"type": "number",
48+
"default": 30,
49+
"description": "When providing type hint items, the number of files searched will be limited by this setting."
50+
}
51+
}
52+
}
4353
},
4454
"scripts": {
45-
"package": "npx vsce package",
46-
"publish": "npx vsce publish",
55+
"package": "vsce package",
56+
"publish": "vsce publish",
4757

4858
"vscode:prepublish": "webpack --mode production",
4959
"compile": "webpack --mode none",

src/completionProvider.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { TypeHintProvider } from "./typeHintProvider";
1313
import { paramHintTrigger, returnHintTrigger, PythonType } from "./python";
1414
import { TypeHint, labelFor } from "./typeHint";
15+
import { TypeHintSettings } from "./settings";
1516

1617

1718
abstract class CompletionProvider {
@@ -32,6 +33,13 @@ abstract class CompletionProvider {
3233
*/
3334
export class ParamHintCompletionProvider extends CompletionProvider implements CompletionItemProvider {
3435

36+
private settings: TypeHintSettings;
37+
38+
constructor(settings: TypeHintSettings) {
39+
super();
40+
this.settings = settings;
41+
}
42+
3543
public async provideCompletionItems(
3644
doc: TextDocument,
3745
pos: Position,
@@ -44,7 +52,7 @@ export class ParamHintCompletionProvider extends CompletionProvider implements C
4452

4553
if (this.shouldProvideItems(line, pos)) {
4654
const param = this.findParam(line, pos);
47-
const provider = new TypeHintProvider(doc);
55+
const provider = new TypeHintProvider(doc, this.settings);
4856

4957
if (param && param.length > 0) {
5058
try {

src/extension.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
import { ParamHintCompletionProvider, ReturnHintCompletionProvider } from './completionProvider';
33
import { paramHintTrigger, returnHintTrigger } from "./python";
4+
import { TypeHintSettings } from './settings';
45

56
// Called when the extension is activated.
67
export function activate(context: vscode.ExtensionContext) {
@@ -9,10 +10,20 @@ export function activate(context: vscode.ExtensionContext) {
910
function registerCommand(commandId: string, func: (...args: any[]) => void): void {
1011
context.subscriptions.push(vscode.commands.registerCommand(commandId, func));
1112
}
12-
13+
14+
const settings = new TypeHintSettings();
15+
1316
context.subscriptions.push(
14-
vscode.languages.registerCompletionItemProvider('python', new ParamHintCompletionProvider(), paramHintTrigger),
15-
vscode.languages.registerCompletionItemProvider('python', new ReturnHintCompletionProvider(), returnHintTrigger)
17+
vscode.languages.registerCompletionItemProvider(
18+
'python',
19+
new ParamHintCompletionProvider(settings),
20+
paramHintTrigger
21+
),
22+
vscode.languages.registerCompletionItemProvider(
23+
'python',
24+
new ReturnHintCompletionProvider(),
25+
returnHintTrigger
26+
)
1627
);
1728
}
1829

src/settings.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { workspace, Event, EventEmitter, window } from "vscode";
2+
3+
export class TypeHintSettings {
4+
5+
private searchLimit = 50;
6+
7+
public get fileSearchLimit() {
8+
return this.searchLimit;
9+
}
10+
11+
constructor() {
12+
workspace.onDidChangeConfiguration(() => {
13+
this.initialize();
14+
this.settingsUpdated.fire();
15+
});
16+
this.initialize();
17+
}
18+
19+
20+
public readonly settingsUpdated = new EventEmitter<void>();
21+
22+
public get onDidChangeConfiguration(): Event<void> {
23+
return this.settingsUpdated.event;
24+
}
25+
26+
private initialize() {
27+
const searchLimit: number | undefined = workspace.getConfiguration('pyTypehint.search').get('limit');
28+
if (searchLimit) {
29+
this.searchLimit = searchLimit;
30+
}
31+
}
32+
33+
}

src/typeHintProvider.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TypeSearch } from "./typeSearch";
44
import { TypingHintProvider } from "./typingHintProvider";
55
import { WorkspaceSearcher } from "./workspaceSearcher";
66
import { TypeHint, labelFor } from "./typeHint";
7+
import { TypeHintSettings } from "./settings";
78

89
/**
910
* Provides type hints.
@@ -22,14 +23,16 @@ export class TypeHintProvider {
2223
private typeContainer: DataTypeContainer = getDataTypeContainer();
2324
private typesIncludedInResult: { [key: string]: string } = {};
2425
private doc: TextDocument;
26+
private settings: TypeHintSettings;
2527

2628
/**
2729
* Constructs a new TypeHintProvider.
2830
*
2931
* @param doc The active document.
3032
*/
31-
constructor(doc: TextDocument) {
33+
constructor(doc: TextDocument, settings: TypeHintSettings) {
3234
this.doc = doc;
35+
this.settings = settings;
3336
}
3437

3538
/**
@@ -47,7 +50,7 @@ export class TypeHintProvider {
4750

4851
const variableSearch = TypeSearch.variableWithSameName(param, documentText);
4952

50-
const wsSearcher = new WorkspaceSearcher(this.doc.uri);
53+
const wsSearcher = new WorkspaceSearcher(this.doc.uri, this.settings);
5154
const wsSearch = wsSearcher.findHintOfSimilarParam(param, documentText);
5255

5356
this.addIfNotNull(TypeSearch.classWithSameName(param, documentText), typeHints);

src/workspaceSearcher.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Uri, workspace } from "vscode";
2+
import { TypeHintSettings } from "./settings";
23
import { TypeSearch } from "./typeSearch";
34

45
/**
@@ -8,16 +9,16 @@ export class WorkspaceSearcher {
89

910
private search: boolean = true;
1011
private activeDocUri: Uri;
11-
12-
private fileSearchLimit = 20; // TODO: config
12+
private settings: TypeHintSettings;
1313

1414
/**
1515
* Constructs a new WorkspaceSearcher.
1616
*
1717
* @param activeDocumentUri The uri of the active document.
1818
*/
19-
constructor(activeDocumentUri: Uri) {
19+
constructor(activeDocumentUri: Uri, settings: TypeHintSettings) {
2020
this.activeDocUri = activeDocumentUri;
21+
this.settings = settings;
2122
}
2223

2324
/**
@@ -31,7 +32,7 @@ export class WorkspaceSearcher {
3132
this.search = true;
3233

3334
if (workspace.workspaceFolders) {
34-
const uris = (await workspace.findFiles("**/*.py", null, this.fileSearchLimit))
35+
const uris = (await workspace.findFiles("**/*.py", null, this.settings.fileSearchLimit))
3536
.filter(u => u.path !== this.activeDocUri.path);
3637

3738
for (let i = 0; this.search && i < uris.length; i++) {

0 commit comments

Comments
 (0)