mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 21:09:43 +00:00
[html] symbol information for embedded CSS and JS
This commit is contained in:
parent
f50074e452
commit
224572723b
|
@ -5,7 +5,8 @@
|
|||
'use strict';
|
||||
|
||||
import { createConnection, IConnection, TextDocuments, InitializeParams, InitializeResult, RequestType } from 'vscode-languageserver';
|
||||
import { DocumentContext, TextDocument, Diagnostic, DocumentLink, Range, TextEdit } from 'vscode-html-languageservice';
|
||||
import { DocumentContext } from 'vscode-html-languageservice';
|
||||
import { TextDocument, Diagnostic, DocumentLink, Range, TextEdit, SymbolInformation } from 'vscode-languageserver-types';
|
||||
import { getLanguageModes, LanguageModes } from './modes/languageModes';
|
||||
|
||||
import * as url from 'url';
|
||||
|
@ -59,6 +60,7 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
|
|||
documentHighlightProvider: true,
|
||||
documentRangeFormattingProvider: initializationOptions && initializationOptions['format.enable'],
|
||||
documentLinkProvider: true,
|
||||
documentSymbolProvider: true,
|
||||
definitionProvider: true,
|
||||
signatureHelpProvider: { triggerCharacters: ['('] },
|
||||
referencesProvider: true
|
||||
|
@ -225,6 +227,17 @@ connection.onDocumentLinks(documentLinkParam => {
|
|||
return links;
|
||||
});
|
||||
|
||||
connection.onDocumentSymbol(documentSymbolParms => {
|
||||
let document = documents.get(documentSymbolParms.textDocument.uri);
|
||||
let symbols: SymbolInformation[] = [];
|
||||
languageModes.getAllModesInDocument(document).forEach(m => {
|
||||
if (m.findDocumentSymbols) {
|
||||
pushAll(symbols, m.findDocumentSymbols(document));
|
||||
}
|
||||
});
|
||||
return symbols;
|
||||
});
|
||||
|
||||
connection.onRequest(ColorSymbolRequest.type, uri => {
|
||||
let ranges: Range[] = [];
|
||||
let document = documents.get(uri);
|
||||
|
|
|
@ -8,7 +8,7 @@ import { LanguageModelCache, getLanguageModelCache } from '../languageModelCache
|
|||
import { TextDocument, Position } from 'vscode-languageserver-types';
|
||||
import { getCSSLanguageService, Stylesheet } from 'vscode-css-languageservice';
|
||||
import { LanguageMode } from './languageModes';
|
||||
import { HTMLDocumentRegions } from './embeddedSupport';
|
||||
import { HTMLDocumentRegions, CSS_STYLE_RULE } from './embeddedSupport';
|
||||
|
||||
export function getCSSMode(documentRegions: LanguageModelCache<HTMLDocumentRegions>): LanguageMode {
|
||||
let cssLanguageService = getCSSLanguageService();
|
||||
|
@ -38,6 +38,10 @@ export function getCSSMode(documentRegions: LanguageModelCache<HTMLDocumentRegio
|
|||
let embedded = embeddedCSSDocuments.get(document);
|
||||
return cssLanguageService.findDocumentHighlights(embedded, position, cssStylesheets.get(embedded));
|
||||
},
|
||||
findDocumentSymbols(document: TextDocument) {
|
||||
let embedded = embeddedCSSDocuments.get(document);
|
||||
return cssLanguageService.findDocumentSymbols(embedded, cssStylesheets.get(embedded)).filter(s => s.name !== CSS_STYLE_RULE);
|
||||
},
|
||||
findDefinition(document: TextDocument, position: Position) {
|
||||
let embedded = embeddedCSSDocuments.get(document);
|
||||
return cssLanguageService.findDefinition(embedded, position, cssStylesheets.get(embedded));
|
||||
|
|
|
@ -20,6 +20,8 @@ export interface HTMLDocumentRegions {
|
|||
getImportedScripts(): string[];
|
||||
}
|
||||
|
||||
export var CSS_STYLE_RULE = '__';
|
||||
|
||||
interface EmbeddedRegion { languageId: string; start: number; end: number; attributeValue?: boolean; };
|
||||
|
||||
|
||||
|
@ -178,7 +180,7 @@ function getEmbeddedDocument(document: TextDocument, contents: EmbeddedRegion[],
|
|||
function getPrefix(c: EmbeddedRegion) {
|
||||
if (c.attributeValue) {
|
||||
switch (c.languageId) {
|
||||
case 'css': return 'x{';
|
||||
case 'css': return CSS_STYLE_RULE + '{';
|
||||
}
|
||||
}
|
||||
return '';
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
'use strict';
|
||||
|
||||
import { LanguageModelCache, getLanguageModelCache } from '../languageModelCache';
|
||||
import { CompletionItem, Location, SignatureHelp, SignatureInformation, ParameterInformation, Definition, TextEdit, TextDocument, Diagnostic, DiagnosticSeverity, Range, CompletionItemKind, Hover, MarkedString, DocumentHighlight, DocumentHighlightKind, CompletionList, Position, FormattingOptions } from 'vscode-languageserver-types';
|
||||
import { SymbolInformation, SymbolKind, CompletionItem, Location, SignatureHelp, SignatureInformation, ParameterInformation, Definition, TextEdit, TextDocument, Diagnostic, DiagnosticSeverity, Range, CompletionItemKind, Hover, MarkedString, DocumentHighlight, DocumentHighlightKind, CompletionList, Position, FormattingOptions } from 'vscode-languageserver-types';
|
||||
import { LanguageMode } from './languageModes';
|
||||
import { getWordAtText } from '../utils/words';
|
||||
import { HTMLDocumentRegions } from './embeddedSupport';
|
||||
|
@ -164,6 +164,42 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
|
|||
};
|
||||
return null;
|
||||
},
|
||||
findDocumentSymbols(document: TextDocument): SymbolInformation[] {
|
||||
currentTextDocument = jsDocuments.get(document);
|
||||
let items = jsLanguageService.getNavigationBarItems(FILE_NAME);
|
||||
if (items) {
|
||||
let result: SymbolInformation[] = [];
|
||||
let existing = {};
|
||||
let collectSymbols = (item: ts.NavigationBarItem, containerLabel?: string) => {
|
||||
let sig = item.text + item.kind + item.spans[0].start;
|
||||
if (item.kind !== 'script' && !existing[sig]) {
|
||||
let symbol: SymbolInformation = {
|
||||
name: item.text,
|
||||
kind: convertSymbolKind(item.kind),
|
||||
location: {
|
||||
uri: document.uri,
|
||||
range: convertRange(currentTextDocument, item.spans[0])
|
||||
},
|
||||
containerName: containerLabel
|
||||
};
|
||||
existing[sig] = true;
|
||||
result.push(symbol);
|
||||
containerLabel = item.text;
|
||||
}
|
||||
|
||||
if (item.childItems && item.childItems.length > 0) {
|
||||
for (let child of item.childItems) {
|
||||
collectSymbols(child, containerLabel);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
items.forEach(item => collectSymbols(item));
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
findDefinition(document: TextDocument, position: Position): Definition {
|
||||
currentTextDocument = jsDocuments.get(document);
|
||||
let definition = jsLanguageService.getDefinitionAtPosition(FILE_NAME, currentTextDocument.offsetAt(position));
|
||||
|
@ -260,6 +296,33 @@ function convertKind(kind: string): CompletionItemKind {
|
|||
return CompletionItemKind.Property;
|
||||
}
|
||||
|
||||
function convertSymbolKind(kind: string): SymbolKind {
|
||||
switch (kind) {
|
||||
case 'var':
|
||||
case 'local var':
|
||||
case 'const':
|
||||
return SymbolKind.Variable;
|
||||
case 'function':
|
||||
case 'local function':
|
||||
return SymbolKind.Function;
|
||||
case 'enum':
|
||||
return SymbolKind.Enum;
|
||||
case 'module':
|
||||
return SymbolKind.Module;
|
||||
case 'class':
|
||||
return SymbolKind.Class;
|
||||
case 'interface':
|
||||
return SymbolKind.Interface;
|
||||
case 'method':
|
||||
return SymbolKind.Method;
|
||||
case 'property':
|
||||
case 'getter':
|
||||
case 'setter':
|
||||
return SymbolKind.Property;
|
||||
}
|
||||
return SymbolKind.Variable;
|
||||
}
|
||||
|
||||
function convertOptions(options: FormattingOptions, formatSettings: any, initialIndentLevel: number): ts.FormatCodeOptions {
|
||||
return {
|
||||
ConvertTabsToSpaces: options.insertSpaces,
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
import { getLanguageService as getHTMLLanguageService, DocumentContext } from 'vscode-html-languageservice';
|
||||
import {
|
||||
CompletionItem, Location, SignatureHelp, Definition, TextEdit, TextDocument, Diagnostic, DocumentLink, Range,
|
||||
Hover, DocumentHighlight, CompletionList, Position, FormattingOptions
|
||||
Hover, DocumentHighlight, CompletionList, Position, FormattingOptions, SymbolInformation
|
||||
} from 'vscode-languageserver-types';
|
||||
|
||||
import { getLanguageModelCache, LanguageModelCache } from '../languageModelCache';
|
||||
|
@ -25,6 +25,7 @@ export interface LanguageMode {
|
|||
doHover?: (document: TextDocument, position: Position) => Hover;
|
||||
doSignatureHelp?: (document: TextDocument, position: Position) => SignatureHelp;
|
||||
findDocumentHighlight?: (document: TextDocument, position: Position) => DocumentHighlight[];
|
||||
findDocumentSymbols?: (document: TextDocument) => SymbolInformation[];
|
||||
findDocumentLinks?: (document: TextDocument, documentContext: DocumentContext) => DocumentLink[];
|
||||
findDefinition?: (document: TextDocument, position: Position) => Definition;
|
||||
findReferences?: (document: TextDocument, position: Position) => Location[];
|
||||
|
|
Loading…
Reference in a new issue