Skip to content

Commit af896c3

Browse files
committed
Remove global WorkspaceIndex (also add command for triggering reindex)
1 parent 97fbc62 commit af896c3

9 files changed

Lines changed: 78 additions & 48 deletions

File tree

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
{
3535
"command": "terraform.lint",
3636
"title": "Terraform: Lint"
37+
},
38+
{
39+
"command": "terraform.reindex",
40+
"title": "Terraform: Reindex terraform files"
3741
}
3842
],
3943
"languages": [
@@ -187,4 +191,4 @@
187191
"dependencies": {
188192
"lodash": "^4.17.10"
189193
}
190-
}
194+
}

src/autocompletion/completion-provider.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import * as vscode from 'vscode';
22
import * as _ from "lodash"
33

4-
import { WorkspaceIndex } from '../index/index';
54
import { terraformConfigAutoComplete, allProviders, IFieldDef } from './model';
5+
import { Index } from '../index';
66

77
const resourceExp = new RegExp("(resource|data)\\s+(\")?(\\w+)(\")?\\s+(\")?([\\w\\-]+)(\")?\\s+({)");
88
const terraformExp = new RegExp("(variable|output|module)\\s+(\")?([\\w\\-]+)(\")?\\s+({)");
@@ -15,8 +15,10 @@ const modules = ["module", "provider"];
1515
const interfaces = ["resource", "data"];
1616

1717
export class CompletionProvider implements vscode.CompletionItemProvider {
18+
constructor(private index: Index) { }
19+
1820
private getVariables(position: vscode.Position, includePrefix: boolean, match?: string): vscode.CompletionItem[] {
19-
return WorkspaceIndex.query("ALL_FILES", { type: "variable", name: match }).map((variable) => {
21+
return this.index.query("ALL_FILES", { type: "variable", name: match }).map((variable) => {
2022
let item = new vscode.CompletionItem(variable.name);
2123
item.kind = vscode.CompletionItemKind.Variable;
2224
if (includePrefix) {
@@ -28,7 +30,7 @@ export class CompletionProvider implements vscode.CompletionItemProvider {
2830
}
2931

3032
private getOutputs(match?: string): vscode.CompletionItem[] {
31-
return WorkspaceIndex.query("ALL_FILES", { type: "output", name: match }).map((output) => {
33+
return this.index.query("ALL_FILES", { type: "output", name: match }).map((output) => {
3234
let item = new vscode.CompletionItem(output.name);
3335
item.kind = vscode.CompletionItemKind.Property;
3436
return item;

src/codelense.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import * as vscode from 'vscode';
22

3-
import { WorkspaceIndex, Section, Reference } from './index/index';
3+
import { Section, Reference, Index } from './index/index';
44

55
export class SectionReferenceCodeLens extends vscode.CodeLens {
66
constructor(
7+
private index: Index,
78
range: vscode.Range,
89
readonly section: Section,
910
command?: vscode.Command) {
1011
super(range, command);
1112
}
1213

1314
createCommand(): vscode.Command {
14-
let references = WorkspaceIndex.queryReferences("ALL_FILES", { target: this.section });
15+
let references = this.index.queryReferences("ALL_FILES", { target: this.section });
1516

1617
return {
1718
title: `${references.length} references`,
@@ -26,16 +27,16 @@ export class CodeLensProvider implements vscode.CodeLensProvider {
2627
private eventEmitter = new vscode.EventEmitter<void>();
2728
onDidChangeCodeLenses = this.eventEmitter.event;
2829

29-
constructor() {
30-
WorkspaceIndex.onDidChange(() => {
30+
constructor(private index: Index) {
31+
this.index.onDidChange(() => {
3132
this.eventEmitter.fire();
3233
});
3334
}
3435

3536
provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): vscode.ProviderResult<vscode.CodeLens[]> {
36-
return WorkspaceIndex.getOrIndexDocument(document).sections.map((s) => {
37+
return this.index.getOrIndexDocument(document).sections.map((s) => {
3738
let firstLineOfSection = new vscode.Range(s.location.range.start, new vscode.Position(s.location.range.start.line, 100000));
38-
return new SectionReferenceCodeLens(firstLineOfSection, s);
39+
return new SectionReferenceCodeLens(this.index, firstLineOfSection, s);
3940
});
4041
}
4142

@@ -67,8 +68,8 @@ export class ReferenceQuickPick implements vscode.QuickPickItem {
6768
}
6869
}
6970

70-
export function showReferencesCommand(section: Section) {
71-
let picks = WorkspaceIndex.queryReferences("ALL_FILES", { target: section }).map((r) => new ReferenceQuickPick(r));
71+
export function showReferencesCommand(index: Index, section: Section) {
72+
let picks = index.queryReferences("ALL_FILES", { target: section }).map((r) => new ReferenceQuickPick(r));
7273

7374
vscode.window.showQuickPick(picks, {
7475
onDidSelectItem: (r: ReferenceQuickPick) => r.goto()

src/extension.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { liveIndex } from './live';
66
import { CompletionProvider } from './autocompletion/completion-provider';
77
import { DefinitionProvider, DocumentSymbolProvider, WorkspaceSymbolProvider, ReferenceProvider, RenameProvider } from './index/providers';
88
import { initialCrawl, createWorkspaceWatcher } from './index/watcher';
9-
import { WorkspaceIndex, Section } from './index';
9+
import { Section, Index } from './index';
1010
import { CodeLensProvider, showReferencesCommand } from './codelense';
1111
import { getConfiguration } from './configuration';
1212
import { HoverProvider } from './hover';
@@ -20,6 +20,8 @@ const documentSelector: vscode.DocumentSelector = [
2020
];
2121

2222
export function activate(ctx: vscode.ExtensionContext) {
23+
let index = new Index();
24+
2325
ctx.subscriptions.push(ErrorDiagnosticCollection);
2426

2527
let formattingProvider = new FormattingEditProvider;
@@ -30,30 +32,35 @@ export function activate(ctx: vscode.ExtensionContext) {
3032
vscode.commands.registerCommand('terraform.validate', () => { validateCommand(); }),
3133
vscode.commands.registerCommand('terraform.lint', () => { lintCommand(); }),
3234
vscode.commands.registerCommand('terraform.showReferences', (section: Section) => {
33-
showReferencesCommand(section);
35+
showReferencesCommand(index, section);
36+
}),
37+
vscode.commands.registerCommand('terraform.reindex', () => {
38+
if (getConfiguration().indexing.enabled) {
39+
initialCrawl(index);
40+
}
3441
}),
3542

3643
// providers
37-
vscode.languages.registerCompletionItemProvider(documentSelector, new CompletionProvider, '.', '"'),
38-
vscode.languages.registerDefinitionProvider(documentSelector, new DefinitionProvider),
39-
vscode.languages.registerDocumentSymbolProvider(documentSelector, new DocumentSymbolProvider),
40-
vscode.languages.registerWorkspaceSymbolProvider(new WorkspaceSymbolProvider),
41-
vscode.languages.registerReferenceProvider(documentSelector, new ReferenceProvider),
42-
vscode.languages.registerRenameProvider(documentSelector, new RenameProvider),
43-
vscode.languages.registerCodeLensProvider(documentSelector, new CodeLensProvider),
44-
vscode.languages.registerHoverProvider(documentSelector, new HoverProvider)
44+
vscode.languages.registerCompletionItemProvider(documentSelector, new CompletionProvider(index), '.', '"'),
45+
vscode.languages.registerDefinitionProvider(documentSelector, new DefinitionProvider(index)),
46+
vscode.languages.registerDocumentSymbolProvider(documentSelector, new DocumentSymbolProvider(index)),
47+
vscode.languages.registerWorkspaceSymbolProvider(new WorkspaceSymbolProvider(index)),
48+
vscode.languages.registerReferenceProvider(documentSelector, new ReferenceProvider(index)),
49+
vscode.languages.registerRenameProvider(documentSelector, new RenameProvider(index)),
50+
vscode.languages.registerCodeLensProvider(documentSelector, new CodeLensProvider(index)),
51+
vscode.languages.registerHoverProvider(documentSelector, new HoverProvider(index))
4552
);
4653

4754
// operations which should only work in a local context (as opposed to live-share)
4855
if (vscode.workspace.rootPath) {
4956
// we need to manually handle save events otherwise format on autosave does not work
5057
ctx.subscriptions.push(vscode.workspace.onDidSaveTextDocument((doc) => formattingProvider.onSave(doc)));
51-
ctx.subscriptions.push(vscode.workspace.onDidChangeTextDocument(liveIndex));
58+
ctx.subscriptions.push(vscode.workspace.onDidChangeTextDocument((e) => liveIndex(index, e)));
5259

5360
// start to build the index
5461
if (getConfiguration().indexing.enabled) {
55-
ctx.subscriptions.push(createWorkspaceWatcher(WorkspaceIndex));
56-
initialCrawl(WorkspaceIndex);
62+
ctx.subscriptions.push(createWorkspaceWatcher(index));
63+
initialCrawl(index);
5764
}
5865
}
5966
}

src/hover.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import * as vscode from 'vscode';
2-
import { WorkspaceIndex, Section } from './index';
2+
import { Section, Index } from './index';
33
import { findValue, getStringValue, getValue } from './index/ast';
44

55
export class HoverProvider implements vscode.HoverProvider {
6+
constructor(private index: Index) { }
7+
68
provideHover(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken): vscode.ProviderResult<vscode.Hover> {
7-
let reference = WorkspaceIndex.queryReferences(document.uri, { position: position })[0];
9+
let reference = this.index.queryReferences(document.uri, { position: position })[0];
810
if (!reference)
911
return null;
1012

11-
let section = WorkspaceIndex.query("ALL_FILES", reference.getQuery())[0];
13+
let section = this.index.query("ALL_FILES", reference.getQuery())[0];
1214
if (section.sectionType !== "variable")
1315
return null;
1416

src/index/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,5 +282,3 @@ export class Index {
282282
return index;
283283
}
284284
}
285-
286-
export let WorkspaceIndex = new Index;

src/index/providers.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,60 @@
11
import * as vscode from 'vscode';
2-
3-
import { WorkspaceIndex } from '../index/index';
4-
2+
import { Index } from '.';
53

64
export class DefinitionProvider implements vscode.DefinitionProvider {
5+
constructor(private index: Index) { }
6+
77
provideDefinition(document: vscode.TextDocument, position: vscode.Position): vscode.Location {
8-
let reference = WorkspaceIndex.queryReferences(document.uri, { position: position })[0];
8+
let reference = this.index.queryReferences(document.uri, { position: position })[0];
99
if (!reference)
1010
return null;
1111

12-
let section = WorkspaceIndex.query("ALL_FILES", reference.getQuery())[0];
12+
let section = this.index.query("ALL_FILES", reference.getQuery())[0];
1313
if (!section)
1414
return null;
1515
return section.location;
1616
}
1717
}
1818

1919
export class ReferenceProvider implements vscode.ReferenceProvider {
20+
constructor(private index: Index) { }
21+
2022
provideReferences(document: vscode.TextDocument, position: vscode.Position, context: vscode.ReferenceContext): vscode.Location[] {
21-
let section = WorkspaceIndex.query(document.uri, { position: position })[0];
23+
let section = this.index.query(document.uri, { position: position })[0];
2224
if (!section)
2325
return [];
2426

25-
let references = WorkspaceIndex.queryReferences("ALL_FILES", { target: section });
27+
let references = this.index.queryReferences("ALL_FILES", { target: section });
2628
return references.map((r) => r.location);
2729
}
2830
}
2931

3032
export class DocumentSymbolProvider implements vscode.DocumentSymbolProvider {
33+
constructor(private index: Index) { }
34+
3135
provideDocumentSymbols(document: vscode.TextDocument): vscode.SymbolInformation[] {
32-
return WorkspaceIndex.query(document.uri);
36+
return this.index.query(document.uri);
3337
}
3438
}
3539

3640
export class WorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvider {
41+
constructor(private index: Index) { }
42+
3743
provideWorkspaceSymbols(query: string): vscode.SymbolInformation[] {
38-
return WorkspaceIndex.query("ALL_FILES", { name: query });
44+
return this.index.query("ALL_FILES", { name: query });
3945
}
4046
}
4147

4248
export class RenameProvider implements vscode.RenameProvider {
49+
constructor(private index: Index) { }
50+
4351
provideRenameEdits(document: vscode.TextDocument, position: vscode.Position, newName: string): vscode.WorkspaceEdit {
44-
let section = WorkspaceIndex.query(document.uri, { name_position: position })[0];
52+
let section = this.index.query(document.uri, { name_position: position })[0];
4553
if (!section) {
4654
return null;
4755
}
4856

49-
let references = WorkspaceIndex.queryReferences("ALL_FILES", { target: section });
57+
let references = this.index.queryReferences("ALL_FILES", { target: section });
5058
if (references.length === 0) {
5159
return null;
5260
}

src/index/watcher.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { parseHcl, ParseError } from './hcl-hil';
55
import { build } from './build';
66
import { ErrorDiagnosticCollection } from '../extension';
77

8-
function updateDocument(index: Index, uri: vscode.Uri) {
9-
vscode.workspace.openTextDocument(uri).then((doc) => {
8+
function updateDocument(index: Index, uri: vscode.Uri): Thenable<void> {
9+
return vscode.workspace.openTextDocument(uri).then((doc) => {
1010
if (doc.isDirty || doc.languageId !== "terraform") {
1111
// ignore
1212
return;
@@ -34,8 +34,16 @@ export function createWorkspaceWatcher(index: Index): vscode.FileSystemWatcher {
3434
export function initialCrawl(index: Index): Thenable<vscode.Uri[]> {
3535
return vscode.workspace.findFiles("**/*.{tf,tfvars}", "")
3636
.then((uris) => {
37-
uris.forEach((uri) => updateDocument(index, uri));
37+
return vscode.window.withProgress({
38+
location: vscode.ProgressLocation.Window,
39+
title: "Indexing terraform templates"
40+
}, async (progress) => {
41+
for (let uri of uris) {
42+
progress.report({ message: `Indexing ${uri.toString()}` });
43+
await updateDocument(index, uri);
44+
}
3845

39-
return uris;
46+
return uris;
47+
});
4048
});
4149
}

src/live.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { execFile } from 'child_process';
44
import { getConfiguration } from './configuration';
55
import { ErrorDiagnosticCollection } from './extension';
66
import { isTerraformDocument } from './helpers';
7-
import { WorkspaceIndex } from './index';
7+
import { Index } from './index';
88

99
let runner;
1010

@@ -17,7 +17,7 @@ function liveIndexEnabledForDocument(doc: vscode.TextDocument): boolean {
1717
return cfg.enabled && cfg.liveIndexing;
1818
}
1919

20-
export function liveIndex(e: vscode.TextDocumentChangeEvent) {
20+
export function liveIndex(index: Index, e: vscode.TextDocumentChangeEvent) {
2121
if (!liveIndexEnabledForDocument(e.document)) {
2222
return;
2323
}
@@ -26,6 +26,6 @@ export function liveIndex(e: vscode.TextDocumentChangeEvent) {
2626
clearTimeout(runner);
2727
}
2828
runner = setTimeout(function () {
29-
WorkspaceIndex.indexDocument(e.document);
29+
index.indexDocument(e.document);
3030
}, getConfiguration().indexing.liveIndexingDelay);
3131
}

0 commit comments

Comments
 (0)