From 66bfcec8cbecc411d464cbf1b9873018a3b1453f Mon Sep 17 00:00:00 2001 From: Tim van der Lippe Date: Fri, 8 Dec 2017 22:23:29 +0100 Subject: [PATCH 1/2] Add initial custom-element standard snippet --- CHANGELOG.md | 1 + src/language-server/auto-completer.ts | 9 +++++++++ src/standard-snippets.ts | 19 +++++++++++++++++++ src/test/auto-completer_test.ts | 3 ++- 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/standard-snippets.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 04d4ebf..c76a380 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). slots of the parent element. - Autocomplete css custom property names. In use sites, complete to any declaration, in declaration sites complete to any declaration or usage. +- Add autocompletion for standard custom element definition. ## 2.0.0 - 2017-12-05 diff --git a/src/language-server/auto-completer.ts b/src/language-server/auto-completer.ts index aa922c1..269a825 100644 --- a/src/language-server/auto-completer.ts +++ b/src/language-server/auto-completer.ts @@ -23,6 +23,7 @@ import {AttributesSection, AttributeValue, TagName, TextNode} from '../ast-from- import AnalyzerLSPConverter from './converter'; import FeatureFinder, {DatabindingFeature} from './feature-finder'; import {Handler} from './util'; +import standardSnippets from '../standard-snippets'; /** @@ -114,6 +115,7 @@ export default class AutoCompleter extends Handler { if (location.kind === 'attribute') { return this.getAttributeCompletions(document, location); } + return this.getStandardSnippetCompletions(); } } @@ -377,6 +379,13 @@ export default class AutoCompleter extends Handler { }; } + private getStandardSnippetCompletions(): CompletionList { + return { + isIncomplete: false, + items: standardSnippets + }; + } + private createSortPrefixes(element: Element): Map { // A map from the inheritedFrom to a sort prefix. Note that // `undefined` is a legal value for inheritedFrom. diff --git a/src/standard-snippets.ts b/src/standard-snippets.ts new file mode 100644 index 0000000..d44d5ce --- /dev/null +++ b/src/standard-snippets.ts @@ -0,0 +1,19 @@ +import {CompletionItemKind, InsertTextFormat} from 'vscode-languageserver'; + +export default [ + { + label: `custom-element`, + documentation: 'Snippet for definition of a custom-element.', + insertText: +`class $1 extends HTMLElement { + constructor() { + super(); + $0 + } +} +customElements.define('$2', $1)`, + insertTextFormat: InsertTextFormat.Snippet, + kind: CompletionItemKind.Class, + filterText: 'customelement', + } +]; diff --git a/src/test/auto-completer_test.ts b/src/test/auto-completer_test.ts index cfcd3dc..11ae017 100644 --- a/src/test/auto-completer_test.ts +++ b/src/test/auto-completer_test.ts @@ -19,6 +19,7 @@ import {ResolvedUrl} from 'polymer-analyzer/lib/model/url'; import {CompletionItem, CompletionItemKind, CompletionList, InsertTextFormat} from 'vscode-languageserver/lib/main'; import {createTestEnvironment} from './util'; +import standardSnippets from '../standard-snippets'; const fixtureDir = path.join(__dirname, '..', '..', 'src', 'test', 'static'); @@ -437,7 +438,7 @@ suite('AutoCompleter', () => { await client.openFile(indexFile, '\n' + indexContents); const completions = await client.getCompletions(indexFile, {line: 1, column: 0}); - assert.deepEqual(completions, {isIncomplete: true, items: []}); + assert.deepEqual(completions, {isIncomplete: false, items: standardSnippets}); }); { From a851ade131aad69bc77243c988842a921e39e976 Mon Sep 17 00:00:00 2001 From: Tim van der Lippe Date: Fri, 8 Dec 2017 23:48:10 +0100 Subject: [PATCH 2/2] Process review feedback --- src/language-server/auto-completer.ts | 12 ++++++++---- src/language-server/feature-finder.ts | 2 +- src/standard-snippets.ts | 17 ++++++++++++++++- src/test/auto-completer_test.ts | 6 +++--- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/language-server/auto-completer.ts b/src/language-server/auto-completer.ts index 269a825..64b20ba 100644 --- a/src/language-server/auto-completer.ts +++ b/src/language-server/auto-completer.ts @@ -23,7 +23,7 @@ import {AttributesSection, AttributeValue, TagName, TextNode} from '../ast-from- import AnalyzerLSPConverter from './converter'; import FeatureFinder, {DatabindingFeature} from './feature-finder'; import {Handler} from './util'; -import standardSnippets from '../standard-snippets'; +import {standardJavaScriptSnippets} from '../standard-snippets'; /** @@ -115,7 +115,11 @@ export default class AutoCompleter extends Handler { if (location.kind === 'attribute') { return this.getAttributeCompletions(document, location); } - return this.getStandardSnippetCompletions(); + // TODO(timvdlippe): Also return these snippets if the user is in a + // javascript file (locResult.language === 'js') + if (location.kind === 'scriptTagContents') { + return this.getStandardJavaScriptSnippetCompletions(); + } } } @@ -379,10 +383,10 @@ export default class AutoCompleter extends Handler { }; } - private getStandardSnippetCompletions(): CompletionList { + private getStandardJavaScriptSnippetCompletions(): CompletionList { return { isIncomplete: false, - items: standardSnippets + items: standardJavaScriptSnippets }; } diff --git a/src/language-server/feature-finder.ts b/src/language-server/feature-finder.ts index 8d5d7bb..e6cde5d 100644 --- a/src/language-server/feature-finder.ts +++ b/src/language-server/feature-finder.ts @@ -12,7 +12,7 @@ * http://polymer.github.io/PATENTS.txt */ -import {Analyzer, Attribute, Document, Element, isPositionInsideRange, Method, ParsedCssDocument, ParsedHtmlDocument, Property, SourcePosition, SourceRange} from 'polymer-analyzer'; +import {Analyzer, Attribute, Document, Element, isPositionInsideRange, Method, ParsedCssDocument, ParsedHtmlDocument, ParsedJavaScriptDocument, Property, SourcePosition, SourceRange} from 'polymer-analyzer'; import {CssCustomPropertyAssignment, CssCustomPropertyUse} from 'polymer-analyzer/lib/css/css-custom-property-scanner'; import {Analysis} from 'polymer-analyzer/lib/model/analysis'; import {DatabindingExpression} from 'polymer-analyzer/lib/polymer/expression-scanner'; diff --git a/src/standard-snippets.ts b/src/standard-snippets.ts index d44d5ce..5af2c59 100644 --- a/src/standard-snippets.ts +++ b/src/standard-snippets.ts @@ -1,6 +1,19 @@ +/** + * Copyright (c) 2017 The Polymer Project Authors. All rights reserved. + * This code may only be used under the BSD style license found at + * http://polymer.github.io/LICENSE.txt + * The complete set of authors may be found at + * http://polymer.github.io/AUTHORS.txt + * The complete set of contributors may be found at + * http://polymer.github.io/CONTRIBUTORS.txt + * Code distributed by Google as part of the polymer project is also + * subject to an additional IP rights grant found at + * http://polymer.github.io/PATENTS.txt + */ + import {CompletionItemKind, InsertTextFormat} from 'vscode-languageserver'; -export default [ +export const standardJavaScriptSnippets = [ { label: `custom-element`, documentation: 'Snippet for definition of a custom-element.', @@ -17,3 +30,5 @@ customElements.define('$2', $1)`, filterText: 'customelement', } ]; + +export default standardJavaScriptSnippets; diff --git a/src/test/auto-completer_test.ts b/src/test/auto-completer_test.ts index 11ae017..cbf09b7 100644 --- a/src/test/auto-completer_test.ts +++ b/src/test/auto-completer_test.ts @@ -19,7 +19,7 @@ import {ResolvedUrl} from 'polymer-analyzer/lib/model/url'; import {CompletionItem, CompletionItemKind, CompletionList, InsertTextFormat} from 'vscode-languageserver/lib/main'; import {createTestEnvironment} from './util'; -import standardSnippets from '../standard-snippets'; +import {standardJavaScriptSnippets} from '../standard-snippets'; const fixtureDir = path.join(__dirname, '..', '..', 'src', 'test', 'static'); @@ -433,12 +433,12 @@ suite('AutoCompleter', () => { {isIncomplete: false, items: attributeCompletions}); }); - test(`Don't give HTML completions inside of script tags.`, async() => { + test(`Return JavaScript standard completions inside of script tags.`, async() => { const {client} = await createTestEnvironment(fixtureDir); await client.openFile(indexFile, '\n' + indexContents); const completions = await client.getCompletions(indexFile, {line: 1, column: 0}); - assert.deepEqual(completions, {isIncomplete: false, items: standardSnippets}); + assert.deepEqual(completions, {isIncomplete: false, items: standardJavaScriptSnippets}); }); {