From 1f343681b70a37a984798e1d2142aa7f35394ff7 Mon Sep 17 00:00:00 2001 From: Tim van der Lippe Date: Tue, 29 Nov 2016 15:44:31 +0100 Subject: [PATCH] Add standard snippets for html import and polymer element definitions --- CHANGELOG.md | 3 +++ src/editor-service.ts | 12 ++++++--- src/local-editor-service.ts | 6 +++-- src/snippets.ts | 48 +++++++++++++++++++++++++++++++++ src/test/editor-service_test.ts | 11 +++++--- 5 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 src/snippets.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index cdb3832..7feee0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +### Added + +* Added standard template definitions for autocompletion. ### Added diff --git a/src/editor-service.ts b/src/editor-service.ts index 0e3b054..840f25a 100644 --- a/src/editor-service.ts +++ b/src/editor-service.ts @@ -18,10 +18,14 @@ export type TypeaheadCompletion = ElementCompletion | AttributesCompletion | AttributeValuesCompletion; export interface ElementCompletion { kind: 'element-tags'; - elements: { - tagname: string, - description: string, expandTo?: string, expandToSnippet?: string - }[]; + elements: ElementSnippet[]; +} +export interface ElementSnippet { + tagname: string; + description: string; + expandTo?: string; + expandToSnippet?: string; + prefix?: string; } export interface AttributesCompletion { kind: 'attributes'; diff --git a/src/local-editor-service.ts b/src/local-editor-service.ts index d154b7c..f7d582c 100644 --- a/src/local-editor-service.ts +++ b/src/local-editor-service.ts @@ -21,6 +21,8 @@ import {Warning, WarningCarryingException} from 'polymer-analyzer/lib/warning/wa import {getLocationInfoForPosition, isPositionInsideRange} from './ast-from-source-position'; import {AttributeCompletion, EditorService, SourcePosition, TypeaheadCompletion} from './editor-service'; +import {snippets} from './snippets'; + export class LocalEditorService extends EditorService { private _analyzer: Analyzer; constructor(options: AnalyzerOptions) { @@ -85,7 +87,7 @@ export class LocalEditorService extends EditorService { Array.from(document.getByKind('element')).filter(e => e.tagName); return { kind: 'element-tags', - elements: elements.map(e => { + elements: snippets.concat(elements.map(e => { const attributesSpace = e.attributes.length > 0 ? ' ' : ''; return { tagname: e.tagName!, @@ -97,7 +99,7 @@ export class LocalEditorService extends EditorService { this._generateAutoCompletionForElement(e) : undefined }; - }) + })) }; } diff --git a/src/snippets.ts b/src/snippets.ts new file mode 100644 index 0000000..473d9e6 --- /dev/null +++ b/src/snippets.ts @@ -0,0 +1,48 @@ +import {ElementSnippet} from './editor-service'; + +const snippets: ElementSnippet[] = [ + { + 'tagname': 'html-import', + 'description': 'Template definition of an HTML import.', + 'prefix': 'hi', + 'expandTo': '', + 'expandToSnippet': '' + }, + { + 'tagname': 'tdd-test', + 'description': 'Template definition for a TDD test suite', + 'prefix': 'tdd-test', + 'expandTo': '', + 'expandToSnippet': ` + + + + $\{1:my-element} + + + + + + + + + + + +` + } +]; + +export {snippets}; diff --git a/src/test/editor-service_test.ts b/src/test/editor-service_test.ts index 7b8522a..0b271b3 100644 --- a/src/test/editor-service_test.ts +++ b/src/test/editor-service_test.ts @@ -28,6 +28,8 @@ import {AttributesCompletion, EditorService, ElementCompletion} from '../editor- import {LocalEditorService} from '../local-editor-service'; import {RemoteEditorService} from '../remote-editor-service'; +import {snippets} from '../snippets'; + chai.use(require('chai-subset')); function editorTests(editorFactory: (basedir: string) => EditorService) { @@ -41,7 +43,7 @@ function editorTests(editorFactory: (basedir: string) => EditorService) { const elementTypeahead: ElementCompletion = { kind: 'element-tags', - elements: [ + elements: snippets.concat([ { tagname: 'behavior-test-elem', description: 'An element to test out behavior inheritance.', @@ -84,13 +86,14 @@ function editorTests(editorFactory: (basedir: string) => EditorService) { expandTo: undefined, expandToSnippet: undefined }, - ] + ]) }; // Like elementTypeahead, but we also want to add a leading < because we're // in a context where we don't have one. const emptyStartElementTypeahead = Object.assign({}, elementTypeahead); emptyStartElementTypeahead.elements = emptyStartElementTypeahead.elements.map(e => { + if (e.prefix) return e; let copy = Object.assign({}, e); let space = ''; const elementsWithAttributes = @@ -520,7 +523,7 @@ function editorTests(editorFactory: (basedir: string) => EditorService) { column: 0 /* after the space after the element name */ }), { - 'elements': [ + 'elements': snippets.concat([ { 'description': '', 'expandTo': '', @@ -538,7 +541,7 @@ function editorTests(editorFactory: (basedir: string) => EditorService) { `$1$0`, 'tagname': 'slot-one-test-elem' } - ], + ]), 'kind': 'element-tags' }); });