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 all 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 @@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- New setting `polymer-ide.logToFile`, a string path to a file. When given,
logs are appended to this file (in addition to possibly going to the
client).
- 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 @@ -24,6 +24,7 @@ import {LsAnalyzer} from './analyzer-synchronizer';
import AnalyzerLSPConverter from './converter';
import FeatureFinder, {DatabindingFeature} from './feature-finder';
import {Handler} from './util';
import {standardJavaScriptSnippets} from '../standard-snippets';


/**
Expand Down Expand Up @@ -115,6 +116,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 @@ -378,6 +384,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
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