Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

Add initial custom-element standard snippet #106

Merged
merged 3 commits into from
Dec 8, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions src/language-server/auto-completer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {standardJavaScriptSnippets} from '../standard-snippets';


/**
Expand Down Expand Up @@ -114,6 +115,11 @@ export default class AutoCompleter extends Handler {
if (location.kind === 'attribute') {
return this.getAttributeCompletions(document, location);
}
// TODO(timvdlippe): Also return these snippets if the user is in a
// javascript file (locResult.language === 'js')
if (location.kind === 'scriptTagContents') {
return this.getStandardJavaScriptSnippetCompletions();
}
}
}

Expand Down Expand Up @@ -377,6 +383,13 @@ export default class AutoCompleter extends Handler {
};
}

private getStandardJavaScriptSnippetCompletions(): CompletionList {
return {
isIncomplete: false,
items: standardJavaScriptSnippets
};
}

private createSortPrefixes(element: Element): Map<string|undefined, string> {
// A map from the inheritedFrom to a sort prefix. Note that
// `undefined` is a legal value for inheritedFrom.
Expand Down
2 changes: 1 addition & 1 deletion src/language-server/feature-finder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
34 changes: 34 additions & 0 deletions src/standard-snippets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License


export const standardJavaScriptSnippets = [
{
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',
}
];

export default standardJavaScriptSnippets;
5 changes: 3 additions & 2 deletions src/test/auto-completer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {standardJavaScriptSnippets} from '../standard-snippets';

const fixtureDir = path.join(__dirname, '..', '..', 'src', 'test', 'static');

Expand Down Expand Up @@ -432,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, '<script>\n\n</script>\n' + indexContents);
const completions =
await client.getCompletions(indexFile, {line: 1, column: 0});
assert.deepEqual(completions, {isIncomplete: true, items: []});
assert.deepEqual(completions, {isIncomplete: false, items: standardJavaScriptSnippets});
});

{
Expand Down