-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.ts
128 lines (111 loc) · 4.76 KB
/
main.ts
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import * as path from 'path';
import {
ExtensionContext,
OutputChannel,
window,
languages,
SnippetString,
commands,
TextEdit,
LocationLink,
Uri,
DefinitionProvider,
TextDocument,
Position,
DefinitionLink,
} from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient';
import AureliaCliCommands from './aureliaCLICommands';
import { RelatedFiles } from './relatedFiles';
import { registerPreview } from './Preview/Register';
import { TextDocumentContentProvider } from './Preview/TextDocumentContentProvider';
import { WebComponent } from '../server/FileParser/Model/WebComponent';
import { getFileNameAsKebabCase } from '../Util/GetFileNameAsKebabCase';
let outputChannel: OutputChannel;
class SearchDefinitionInViewV2 implements DefinitionProvider {
client: LanguageClient;
constructor(client: LanguageClient) {
this.client = client;
}
public async provideDefinition(
document: TextDocument,
position: Position): Promise<DefinitionLink[]> {
const { definitionsInfo, definitionsAttributesInfo } = await this.client.sendRequest('aurelia-definition-provide');
const goToSourceWordRange = document.getWordRangeAtPosition(position);
const goToSourceWord = document.getText(goToSourceWordRange);
const foundDefinitions = definitionsInfo[goToSourceWord];
const getFileName = (path: string): string => {
return path.split('/').pop().replace(/\..+/, '');
}
const currentFileName = getFileName(document.fileName);
const possibleDefs = foundDefinitions.filter(foundDef => {
const isCustomElement = getFileName(foundDef.targetUri) === goToSourceWord;
const isViewModelVariable = getFileName(foundDef.targetUri) === currentFileName; // eg. `.bind="viewModelVariable"`
const isBindingAttribute = definitionsAttributesInfo[goToSourceWord] // eg. `binding-attribute.bind="..."`
return isCustomElement || isViewModelVariable || isBindingAttribute;
});
let targetDef;
if (possibleDefs.length === 1) {
targetDef = possibleDefs[0];
} else {
targetDef = possibleDefs.find(possibleDef => {
const attrInfos = definitionsAttributesInfo[goToSourceWord] // eg. `binding-attribute.bind="..."`
const isBindingAttribute = attrInfos[0].customElementName === getFileName(possibleDef.targetUri);
return isBindingAttribute;
});
}
return {
...targetDef,
targetUri: Uri.file(targetDef.targetUri)
}
}
}
export function activate(context: ExtensionContext) {
// Create default output channel
outputChannel = window.createOutputChannel('aurelia');
context.subscriptions.push(outputChannel);
// Register CLI commands
context.subscriptions.push(AureliaCliCommands.registerCommands(outputChannel));
context.subscriptions.push(new RelatedFiles());
// Register Code Actions
const edit = (uri: string, documentVersion: number, edits: TextEdit[]) => {
let textEditor = window.activeTextEditor;
if (textEditor && textEditor.document.uri.toString() === uri) {
textEditor.edit(mutator => {
for (let edit of edits) {
mutator.replace(client.protocol2CodeConverter.asRange(edit.range), edit.newText);
}
}).then((success) => {
window.activeTextEditor.document.save();
if (!success) {
window.showErrorMessage('Failed to apply Aurelia code fixes to the document. Please consider opening an issue with steps to reproduce.');
}
});
}
};
context.subscriptions.push(commands.registerCommand('aurelia-attribute-invalid-case', edit));
context.subscriptions.push(commands.registerCommand('aurelia-binding-one-way-deprecated', edit));
// Register Aurelia language server
const serverModule = context.asAbsolutePath(path.join('dist', 'src', 'server', 'main.js'));
const debugOptions = { execArgv: ['--nolazy', '--inspect=6100'] };
const serverOptions: ServerOptions = {
debug: { module: serverModule, options: debugOptions, transport: TransportKind.ipc },
run: { module: serverModule, transport: TransportKind.ipc },
};
const clientOptions: LanguageClientOptions = {
diagnosticCollectionName: 'Aurelia',
documentSelector: ['html'],
initializationOptions: {},
synchronize: {
configurationSection: ['aurelia'],
},
};
const client = new LanguageClient('html', 'Aurelia', serverOptions, clientOptions);
registerPreview(context, window, client);
context.subscriptions.push(languages.registerDefinitionProvider(
{ scheme: 'file', language: 'html' },
new SearchDefinitionInViewV2(client))
);
const disposable = client.start();
context.subscriptions.push(disposable);
}