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 1 commit
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
9 changes: 9 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 standardSnippets from '../standard-snippets';


/**
Expand Down Expand Up @@ -114,6 +115,7 @@ export default class AutoCompleter extends Handler {
if (location.kind === 'attribute') {
return this.getAttributeCompletions(document, location);
}
return this.getStandardSnippetCompletions();
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, should explicitly check for the location being inside of a script section, as we might add other location kinds.

For bonus points it would be cool to do to JS what we did for CSS, and have an entire class of result for locResult.language === js but that might be more than you're interested in doing right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I left a TODO, as I tried to make that work, but it required new AST traversal methods in ast-from-source-position, which seemed too much for this PR. Should be fixed someday though 😄

}
}

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

private getStandardSnippetCompletions(): CompletionList {
return {
isIncomplete: false,
items: standardSnippets
};
}

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
19 changes: 19 additions & 0 deletions src/standard-snippets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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 default [
Copy link
Contributor

Choose a reason for hiding this comment

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

Recommend giving this a name, as these are specifically snippets for when the user is in a JS file.

{
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',
}
];
3 changes: 2 additions & 1 deletion 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 standardSnippets from '../standard-snippets';

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

Expand Down Expand Up @@ -437,7 +438,7 @@ suite('AutoCompleter', () => {
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: standardSnippets});
Copy link
Contributor

Choose a reason for hiding this comment

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

Update the test name

});

{
Expand Down