html.experimental.custom.tags/attributes for Microsoft/vscode#62976

This commit is contained in:
Pine Wu
2018-11-28 16:34:12 -08:00
parent 3725e59112
commit d198091b68
7 changed files with 139 additions and 16 deletions

View File

@@ -11,6 +11,7 @@ import {
} from 'vscode-languageserver';
import { TextDocument, Diagnostic, DocumentLink, SymbolInformation } from 'vscode-languageserver-types';
import { getLanguageModes, LanguageModes, Settings } from './modes/languageModes';
import * as fs from 'fs';
import { format } from './modes/formatting';
import { pushAll } from './utils/arrays';
@@ -19,6 +20,8 @@ import uri from 'vscode-uri';
import { formatError, runSafe, runSafeAsync } from './utils/runner';
import { getFoldingRanges } from './modes/htmlFolding';
import { parseTagSet, parseAttributes } from './utils/tagDefinitions';
import { ITagSet, IAttributeSet } from 'vscode-html-languageservice';
namespace TagCloseRequest {
export const type: RequestType<TextDocumentPositionParams, string | null, any, any> = new RequestType('html/tag');
@@ -88,11 +91,47 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
}
}
const tagPaths: string[] = params.initializationOptions.tagPaths;
const attributePaths: string[] = params.initializationOptions.attributePaths;
const htmlTags: ITagSet = {};
const htmlAttributes: IAttributeSet = {};
if (tagPaths) {
tagPaths.forEach(path => {
try {
if (fs.existsSync(path)) {
const tagSet = parseTagSet(fs.readFileSync(path, 'utf-8'));
for (let tag in tagSet) {
htmlTags[tag] = tagSet[tag];
}
}
} catch (err) {
console.log(`Failed to laod tag from ${path}`);
}
});
}
if (htmlAttributes) {
attributePaths.forEach(path => {
try {
if (fs.existsSync(path)) {
const attributeSet = parseAttributes(fs.readFileSync(path, 'utf-8'));
for (let ga in attributeSet) {
htmlAttributes[ga] = attributeSet[ga];
}
}
} catch (err) {
console.log(`Failed to load attributes from ${path}`);
}
});
}
const workspace = {
get settings() { return globalSettings; },
get folders() { return workspaceFolders; }
};
languageModes = getLanguageModes(initializationOptions ? initializationOptions.embeddedLanguages : { css: true, javascript: true }, workspace);
languageModes = getLanguageModes(initializationOptions ? initializationOptions.embeddedLanguages : { css: true, javascript: true }, workspace, htmlTags, htmlAttributes);
documents.onDidClose(e => {
languageModes.onDocumentRemoved(e.document);
});

View File

@@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { getLanguageService as getHTMLLanguageService, DocumentContext } from 'vscode-html-languageservice';
import { getLanguageService as getHTMLLanguageService, DocumentContext, ITagSet, IAttributeSet } from 'vscode-html-languageservice';
import {
CompletionItem, Location, SignatureHelp, Definition, TextEdit, TextDocument, Diagnostic, DocumentLink, Range,
Hover, DocumentHighlight, CompletionList, Position, FormattingOptions, SymbolInformation, FoldingRange
@@ -65,9 +65,13 @@ export interface LanguageModeRange extends Range {
attributeValue?: boolean;
}
export function getLanguageModes(supportedLanguages: { [languageId: string]: boolean; }, workspace: Workspace): LanguageModes {
export function getLanguageModes(supportedLanguages: { [languageId: string]: boolean; }, workspace: Workspace, customTags?: ITagSet, customAttributes?: IAttributeSet): LanguageModes {
var htmlLanguageService = getHTMLLanguageService({
customTags,
customAttributes
});
var htmlLanguageService = getHTMLLanguageService();
let documentRegions = getLanguageModelCache<HTMLDocumentRegions>(10, 60, document => getDocumentRegions(htmlLanguageService, document));
let modelCaches: LanguageModelCache<any>[] = [];

View File

@@ -0,0 +1,58 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { ITagSet, IAttributeSet, HTMLTagSpecification } from 'vscode-html-languageservice';
interface Tag {
label: string;
description: string;
attributes: Attribute[];
}
interface Attribute {
label: string;
description: string;
}
interface RawTagSet {
tags: Tag[];
}
interface RawAttributeSet {
attributes: Attribute[];
}
export function parseTagSet(source: string): ITagSet {
const tagSet: ITagSet = {};
let rawTagSet: RawTagSet;
try {
rawTagSet = JSON.parse(source);
} catch (err) {
return {};
}
rawTagSet.tags.forEach(c => {
tagSet[c.label] = new HTMLTagSpecification(c.description, c.attributes.map(a => a.label));
});
return tagSet;
}
export function parseAttributes(source: string): IAttributeSet {
const attributeSet: IAttributeSet = {};
let rawAttributeSet: RawAttributeSet;
try {
rawAttributeSet = JSON.parse(source);
} catch (err) {
return {};
}
rawAttributeSet.attributes.forEach(ag => {
attributeSet[ag.label] = {
...ag
};
});
return attributeSet;
}