Skip to content

Commit 7a0b459

Browse files
committed
wip(ls): filter html elements when extending html complitions
fix uri interpretation causing crashes feat surface new completion types
1 parent cb13417 commit 7a0b459

File tree

4 files changed

+40
-15
lines changed

4 files changed

+40
-15
lines changed

client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ng-template",
33
"displayName": "Angular Template",
44
"description": "Angular Template Service",
5-
"version": "0.0.1",
5+
"version": "0.0.4",
66
"publisher": "Google",
77
"engines": {
88
"vscode": "^1.4.0"

server/src/documents.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,28 @@ class ProjectLoggerImpl implements ProjectLogger {
100100
}
101101
}
102102

103+
function removePrefix(value: string, ...prefixes: string[]): string {
104+
for (const prefix of prefixes) {
105+
if (value && value.startsWith(prefix)) {
106+
return value.substr(prefix.length);
107+
}
108+
}
109+
return value;
110+
}
111+
112+
const privateProtocol = "private:";
103113
const fileProtocol = "file://";
104114
function uriToFileName(uri: string): string {
105-
if (uri && uri.startsWith(fileProtocol)) {
106-
return uri.substr(fileProtocol.length);
107-
}
108-
return uri;
115+
return removePrefix(decodeURI(uri), fileProtocol, privateProtocol);
109116
}
117+
110118
function fileNameToUri(fileName: string): string {
111-
return fileProtocol + fileName;
119+
return encodeURI(fileProtocol + fileName);
112120
}
113121

114122
export interface NgServiceInfo {
115123
fileName: string;
124+
languageId?: string;
116125
service?: LanguageService;
117126
offset?: number;
118127
}
@@ -126,6 +135,7 @@ export class TextDocuments {
126135
private projectService: ProjectService;
127136
private logger: ProjectLoggerImpl;
128137
private host: ProjectServiceHostImpl;
138+
private languageIds = new Map<string, string>();
129139
private changeNumber = 0;
130140

131141
constructor(private event?: (event: TextDocumentEvent) => void) {
@@ -150,6 +160,7 @@ export class TextDocuments {
150160
// TODO: Report errors
151161
this.logger.msg(`Config errors encountered and need to be reported: ${configFileErrors.length}\n ${configFileErrors.map(error => error.messageText).join('\n ')}`);
152162
}
163+
this.languageIds.set(event.textDocument.uri, event.textDocument.languageId);
153164
});
154165

155166
connection.onDidCloseTextDocument(event => {
@@ -203,16 +214,17 @@ export class TextDocuments {
203214
public getServiceInfo(document: TextDocumentIdentifier, position?: Position): NgServiceInfo {
204215
const fileName = uriToFileName(document.uri);
205216
const project = this.projectService.getProjectForFile(fileName);
217+
const languageId = this.languageIds.get(document.uri);
206218
if (project) {
207219
const service = project.compilerService.ngService;
208220
if (position) {
209221
// VSCode is 0 based, editor services are 1 based.
210222
const offset = this.projectService.lineOffsetsToPositions(fileName, [{line: position.line + 1, col: position.character + 1}])[0];
211-
return {fileName, service, offset};
223+
return {fileName, service, offset, languageId};
212224
}
213-
return {fileName, service};
225+
return {fileName, service, languageId};
214226
}
215-
return {fileName};
227+
return {fileName, languageId};
216228
}
217229

218230
public ifUnchanged(f: () => void): () => void {

server/src/server.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,32 @@ connection.onInitialize((params): InitializeResult => {
6464

6565
function compiletionKindToCompletionItemKind(kind: string): number {
6666
switch (kind) {
67+
case 'attribute': return CompletionItemKind.Property;
68+
case 'html attribute': return CompletionItemKind.Property;
69+
case 'component': return CompletionItemKind.Class;
6770
case 'element': return CompletionItemKind.Class;
68-
case 'attribute': return CompletionItemKind.Field;
6971
case 'entity': return CompletionItemKind.Text;
70-
case 'member': return CompletionItemKind.Property;
72+
case 'key': return CompletionItemKind.Class;
73+
case 'method': return CompletionItemKind.Method;
74+
case 'pipe': return CompletionItemKind.Function;
75+
case 'property': return CompletionItemKind.Property;
76+
case 'type': return CompletionItemKind.Interface;
77+
case 'reference': return CompletionItemKind.Variable;
78+
case 'variable': return CompletionItemKind.Variable;
7179
}
7280
return CompletionItemKind.Text;
7381
}
7482

7583
// This handler provides the initial list of the completion items.
7684
connection.onCompletion((textDocumentPosition: TextDocumentPositionParams): CompletionItem[] => {
77-
const {fileName, service, offset} = documents.getServiceInfo(textDocumentPosition.textDocument,
85+
const {fileName, service, offset, languageId} = documents.getServiceInfo(textDocumentPosition.textDocument,
7886
textDocumentPosition.position)
79-
if (service) {
80-
const result = service.getCompletionsAt(fileName, offset);
87+
if (service && offset != null) {
88+
let result = service.getCompletionsAt(fileName, offset);
89+
if (result && languageId == 'html') {
90+
// The HTML elements are provided by the HTML service when the text type is 'html'.
91+
result = result.filter(completion => completion.kind != 'element');
92+
}
8193
if (result) {
8294
return result.map(completion => ({
8395
label: completion.name,

server/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"moduleResolution": "node",
66
"sourceMap": true,
77
"outDir": "../client/server",
8-
"lib": ["es5", "es2015.core", "es2015.promise", "es2015.collection", "es2015.iterable", "dom"]
8+
"lib": ["es5", "es2015.core", "es2015.promise", "es2015.collection", "es2015.iterable", "dom"],
9+
"types": []
910
},
1011
"exclude": [
1112
"node_modules"

0 commit comments

Comments
 (0)