-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathautocomplete.ts
More file actions
102 lines (83 loc) · 3.98 KB
/
autocomplete.ts
File metadata and controls
102 lines (83 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/************************************************************************************************
*
* MIT License
*
* Copyright (c) 2020 Raghuveer S
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
* Author@Raghuveer S, 2019
* This is a plugin that I use with VS Code to enable autocompletion of
* filenames when using wiki-style link syntax "[["
*
* How to use this file:
* ---------------------
* Disclaimer: This is not a published plugin i.e., it is not present in the VS Code Marketplace.
* 1. Create a VSCode Plugin template using Yeoman. (This can be googled, it's a very simple step)
* 2. Now open the template folder that you created.
* 3. Copy this file to the 'src' folder inside the template folder and rename it to 'extension.ts'.
* If there is already a file by that name in the 'src', just replace it with this.
* 4. Now copy the entire template folder to C:/Users/<UserName>/.vscode/
* 5. Restart vscode and you should now find that when you are working with 'simply jekyll' posts,
* you have autocompletion ready to fire.
**************************************************************************************************/
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as path from 'path';
export function activate(context: vscode.ExtensionContext) {
const provider = new IncludeCompletionProvider();
context.subscriptions.push(vscode.languages.registerCompletionItemProvider('markdown', provider, '['));
}
class IncludeCompletionProvider implements vscode.CompletionItemProvider, vscode.Disposable {
private titles: string[] = [];
private watcher: vscode.FileSystemWatcher;
constructor() {
this.updateTitles();
this.watcher = vscode.workspace.createFileSystemWatcher("**/_site/autocomplete.txt");
this.watcher.onDidCreate(()=> this.updateTitles());
this.watcher.onDidChange(()=> this.updateTitles());
this.watcher.onDidDelete(()=> this.updateTitles());
}
public dispose() {
this.watcher.dispose();
}
public provideCompletionItems (document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken) {
let linePrefix = document.lineAt(position).text.substr(0, position.character);
if (!linePrefix.endsWith('[[')) {
return undefined;
}
let completionItemArray: vscode.CompletionItem[] = [];
for (let entry of this.titles) {
completionItemArray.push(new vscode.CompletionItem(entry, vscode.CompletionItemKind.Text));
}
return completionItemArray;
}
private async updateTitles() {
if (!vscode.workspace.workspaceFolders) {
return undefined;
}
const folderUri = vscode.workspace.workspaceFolders[0].uri;
const fileUri = folderUri.with({ path: path.posix.join(folderUri.path, '_site/autocomplete.txt') });
let titles = <string[]> undefined;
const readData = await vscode.workspace.fs.readFile(fileUri);
titles = Buffer.from(readData).toString('utf8').split(";");
this.titles = titles;
}
}