123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- 'use strict';
- import { LanguageServiceDefaults } from './monaco.contribution';
- import type * as ts from './lib/typescriptServices';
- import type { TypeScriptWorker } from './tsWorker';
- import { libFileSet } from './lib/lib.index';
- import {
- editor,
- languages,
- Uri,
- Position,
- Range,
- CancellationToken,
- IDisposable,
- IRange,
- MarkerTag,
- MarkerSeverity
- } from './fillers/monaco-editor-core';
- //#region utils copied from typescript to prevent loading the entire typescriptServices ---
- enum IndentStyle {
- None = 0,
- Block = 1,
- Smart = 2
- }
- export function flattenDiagnosticMessageText(
- diag: string | ts.DiagnosticMessageChain | undefined,
- newLine: string,
- indent = 0
- ): string {
- if (typeof diag === 'string') {
- return diag;
- } else if (diag === undefined) {
- return '';
- }
- let result = '';
- if (indent) {
- result += newLine;
- for (let i = 0; i < indent; i++) {
- result += ' ';
- }
- }
- result += diag.messageText;
- indent++;
- if (diag.next) {
- for (const kid of diag.next) {
- result += flattenDiagnosticMessageText(kid, newLine, indent);
- }
- }
- return result;
- }
- function displayPartsToString(
- displayParts: ts.SymbolDisplayPart[] | undefined
- ): string {
- if (displayParts) {
- return displayParts.map((displayPart) => displayPart.text).join('');
- }
- return '';
- }
- //#endregion
- export abstract class Adapter {
- constructor(
- protected _worker: (...uris: Uri[]) => Promise<TypeScriptWorker>
- ) {}
- // protected _positionToOffset(model: editor.ITextModel, position: monaco.IPosition): number {
- // return model.getOffsetAt(position);
- // }
- // protected _offsetToPosition(model: editor.ITextModel, offset: number): monaco.IPosition {
- // return model.getPositionAt(offset);
- // }
- protected _textSpanToRange(
- model: editor.ITextModel,
- span: ts.TextSpan
- ): IRange {
- let p1 = model.getPositionAt(span.start);
- let p2 = model.getPositionAt(span.start + span.length);
- let { lineNumber: startLineNumber, column: startColumn } = p1;
- let { lineNumber: endLineNumber, column: endColumn } = p2;
- return { startLineNumber, startColumn, endLineNumber, endColumn };
- }
- }
- // --- lib files
- export class LibFiles {
- private _libFiles: Record<string, string>;
- private _hasFetchedLibFiles: boolean;
- private _fetchLibFilesPromise: Promise<void> | null;
- constructor(
- private readonly _worker: (...uris: Uri[]) => Promise<TypeScriptWorker>
- ) {
- this._libFiles = {};
- this._hasFetchedLibFiles = false;
- this._fetchLibFilesPromise = null;
- }
- public isLibFile(uri: Uri | null): boolean {
- if (!uri) {
- return false;
- }
- if (uri.path.indexOf('/lib.') === 0) {
- return !!libFileSet[uri.path.slice(1)];
- }
- return false;
- }
- public getOrCreateModel(uri: Uri): editor.ITextModel | null {
- const model = editor.getModel(uri);
- if (model) {
- return model;
- }
- if (this.isLibFile(uri) && this._hasFetchedLibFiles) {
- return editor.createModel(
- this._libFiles[uri.path.slice(1)],
- 'javascript',
- uri
- );
- }
- return null;
- }
- private _containsLibFile(uris: (Uri | null)[]): boolean {
- for (let uri of uris) {
- if (this.isLibFile(uri)) {
- return true;
- }
- }
- return false;
- }
- public async fetchLibFilesIfNecessary(uris: (Uri | null)[]): Promise<void> {
- if (!this._containsLibFile(uris)) {
- // no lib files necessary
- return;
- }
- await this._fetchLibFiles();
- }
- private _fetchLibFiles(): Promise<void> {
- if (!this._fetchLibFilesPromise) {
- this._fetchLibFilesPromise = this._worker()
- .then((w) => w.getLibFiles())
- .then((libFiles) => {
- this._hasFetchedLibFiles = true;
- this._libFiles = libFiles;
- });
- }
- return this._fetchLibFilesPromise;
- }
- }
- // --- diagnostics --- ---
- enum DiagnosticCategory {
- Warning = 0,
- Error = 1,
- Suggestion = 2,
- Message = 3
- }
- export class DiagnosticsAdapter extends Adapter {
- private _disposables: IDisposable[] = [];
- private _listener: { [uri: string]: IDisposable } = Object.create(null);
- constructor(
- private readonly _libFiles: LibFiles,
- private _defaults: LanguageServiceDefaults,
- private _selector: string,
- worker: (...uris: Uri[]) => Promise<TypeScriptWorker>
- ) {
- super(worker);
- const onModelAdd = (model: editor.IModel): void => {
- if (model.getModeId() !== _selector) {
- return;
- }
- let handle: number;
- const changeSubscription = model.onDidChangeContent(() => {
- clearTimeout(handle);
- handle = setTimeout(() => this._doValidate(model), 500);
- });
- this._listener[model.uri.toString()] = {
- dispose() {
- changeSubscription.dispose();
- clearTimeout(handle);
- }
- };
- this._doValidate(model);
- };
- const onModelRemoved = (model: editor.IModel): void => {
- editor.setModelMarkers(model, this._selector, []);
- const key = model.uri.toString();
- if (this._listener[key]) {
- this._listener[key].dispose();
- delete this._listener[key];
- }
- };
- this._disposables.push(editor.onDidCreateModel(onModelAdd));
- this._disposables.push(editor.onWillDisposeModel(onModelRemoved));
- this._disposables.push(
- editor.onDidChangeModelLanguage((event) => {
- onModelRemoved(event.model);
- onModelAdd(event.model);
- })
- );
- this._disposables.push({
- dispose() {
- for (const model of editor.getModels()) {
- onModelRemoved(model);
- }
- }
- });
- const recomputeDiagostics = () => {
- // redo diagnostics when options change
- for (const model of editor.getModels()) {
- onModelRemoved(model);
- onModelAdd(model);
- }
- };
- this._disposables.push(this._defaults.onDidChange(recomputeDiagostics));
- this._disposables.push(
- this._defaults.onDidExtraLibsChange(recomputeDiagostics)
- );
- editor.getModels().forEach(onModelAdd);
- }
- public dispose(): void {
- this._disposables.forEach((d) => d && d.dispose());
- this._disposables = [];
- }
- private async _doValidate(model: editor.ITextModel): Promise<void> {
- const worker = await this._worker(model.uri);
- if (model.isDisposed()) {
- // model was disposed in the meantime
- return;
- }
- const promises: Promise<ts.Diagnostic[]>[] = [];
- const {
- noSyntaxValidation,
- noSemanticValidation,
- noSuggestionDiagnostics
- } = this._defaults.getDiagnosticsOptions();
- if (!noSyntaxValidation) {
- promises.push(worker.getSyntacticDiagnostics(model.uri.toString()));
- }
- if (!noSemanticValidation) {
- promises.push(worker.getSemanticDiagnostics(model.uri.toString()));
- }
- if (!noSuggestionDiagnostics) {
- promises.push(worker.getSuggestionDiagnostics(model.uri.toString()));
- }
- const allDiagnostics = await Promise.all(promises);
- if (!allDiagnostics || model.isDisposed()) {
- // model was disposed in the meantime
- return;
- }
- const diagnostics = allDiagnostics
- .reduce((p, c) => c.concat(p), [])
- .filter(
- (d) =>
- (
- this._defaults.getDiagnosticsOptions().diagnosticCodesToIgnore || []
- ).indexOf(d.code) === -1
- );
- // Fetch lib files if necessary
- const relatedUris = diagnostics
- .map((d) => d.relatedInformation || [])
- .reduce((p, c) => c.concat(p), [])
- .map((relatedInformation) =>
- relatedInformation.file
- ? Uri.parse(relatedInformation.file.fileName)
- : null
- );
- await this._libFiles.fetchLibFilesIfNecessary(relatedUris);
- if (model.isDisposed()) {
- // model was disposed in the meantime
- return;
- }
- editor.setModelMarkers(
- model,
- this._selector,
- diagnostics.map((d) => this._convertDiagnostics(model, d))
- );
- }
- private _convertDiagnostics(
- model: editor.ITextModel,
- diag: ts.Diagnostic
- ): editor.IMarkerData {
- const diagStart = diag.start || 0;
- const diagLength = diag.length || 1;
- const {
- lineNumber: startLineNumber,
- column: startColumn
- } = model.getPositionAt(diagStart);
- const {
- lineNumber: endLineNumber,
- column: endColumn
- } = model.getPositionAt(diagStart + diagLength);
- const tags: MarkerTag[] = [];
- if (diag.reportsUnnecessary) {
- tags.push(MarkerTag.Unnecessary);
- }
- if (diag.reportsDeprecated) {
- tags.push(MarkerTag.Deprecated);
- }
- return {
- severity: this._tsDiagnosticCategoryToMarkerSeverity(diag.category),
- startLineNumber,
- startColumn,
- endLineNumber,
- endColumn,
- message: flattenDiagnosticMessageText(diag.messageText, '\n'),
- code: diag.code.toString(),
- tags,
- relatedInformation: this._convertRelatedInformation(
- model,
- diag.relatedInformation
- )
- };
- }
- private _convertRelatedInformation(
- model: editor.ITextModel,
- relatedInformation?: ts.DiagnosticRelatedInformation[]
- ): editor.IRelatedInformation[] | undefined {
- if (!relatedInformation) {
- return;
- }
- const result: editor.IRelatedInformation[] = [];
- relatedInformation.forEach((info) => {
- let relatedResource: editor.ITextModel | null = model;
- if (info.file) {
- const relatedResourceUri = Uri.parse(info.file.fileName);
- relatedResource = this._libFiles.getOrCreateModel(relatedResourceUri);
- }
- if (!relatedResource) {
- return;
- }
- const infoStart = info.start || 0;
- const infoLength = info.length || 1;
- const {
- lineNumber: startLineNumber,
- column: startColumn
- } = relatedResource.getPositionAt(infoStart);
- const {
- lineNumber: endLineNumber,
- column: endColumn
- } = relatedResource.getPositionAt(infoStart + infoLength);
- result.push({
- resource: relatedResource.uri,
- startLineNumber,
- startColumn,
- endLineNumber,
- endColumn,
- message: flattenDiagnosticMessageText(info.messageText, '\n')
- });
- });
- return result;
- }
- private _tsDiagnosticCategoryToMarkerSeverity(
- category: ts.DiagnosticCategory
- ): MarkerSeverity {
- switch (category) {
- case DiagnosticCategory.Error:
- return MarkerSeverity.Error;
- case DiagnosticCategory.Message:
- return MarkerSeverity.Info;
- case DiagnosticCategory.Warning:
- return MarkerSeverity.Warning;
- case DiagnosticCategory.Suggestion:
- return MarkerSeverity.Hint;
- }
- return MarkerSeverity.Info;
- }
- }
- // --- suggest ------
- interface MyCompletionItem extends languages.CompletionItem {
- label: string;
- uri: Uri;
- position: Position;
- }
- export class SuggestAdapter
- extends Adapter
- implements languages.CompletionItemProvider {
- public get triggerCharacters(): string[] {
- return ['.'];
- }
- public async provideCompletionItems(
- model: editor.ITextModel,
- position: Position,
- _context: languages.CompletionContext,
- token: CancellationToken
- ): Promise<languages.CompletionList | undefined> {
- const wordInfo = model.getWordUntilPosition(position);
- const wordRange = new Range(
- position.lineNumber,
- wordInfo.startColumn,
- position.lineNumber,
- wordInfo.endColumn
- );
- const resource = model.uri;
- const offset = model.getOffsetAt(position);
- const worker = await this._worker(resource);
- const info = await worker.getCompletionsAtPosition(
- resource.toString(),
- offset
- );
- if (!info || model.isDisposed()) {
- return;
- }
- const suggestions: MyCompletionItem[] = info.entries.map((entry) => {
- let range = wordRange;
- if (entry.replacementSpan) {
- const p1 = model.getPositionAt(entry.replacementSpan.start);
- const p2 = model.getPositionAt(
- entry.replacementSpan.start + entry.replacementSpan.length
- );
- range = new Range(p1.lineNumber, p1.column, p2.lineNumber, p2.column);
- }
- const tags: languages.CompletionItemTag[] = [];
- if (entry.kindModifiers?.indexOf('deprecated') !== -1) {
- tags.push(languages.CompletionItemTag.Deprecated);
- }
- return {
- uri: resource,
- position: position,
- range: range,
- label: entry.name,
- insertText: entry.name,
- sortText: entry.sortText,
- kind: SuggestAdapter.convertKind(entry.kind),
- tags
- };
- });
- return {
- suggestions
- };
- }
- public async resolveCompletionItem(
- model: editor.ITextModel,
- _position: Position,
- item: languages.CompletionItem,
- token: CancellationToken
- ): Promise<languages.CompletionItem> {
- const myItem = <MyCompletionItem>item;
- const resource = myItem.uri;
- const position = myItem.position;
- const offset = model.getOffsetAt(position);
- const worker = await this._worker(resource);
- const details = await worker.getCompletionEntryDetails(
- resource.toString(),
- offset,
- myItem.label
- );
- if (!details || model.isDisposed()) {
- return myItem;
- }
- return <MyCompletionItem>{
- uri: resource,
- position: position,
- label: details.name,
- kind: SuggestAdapter.convertKind(details.kind),
- detail: displayPartsToString(details.displayParts),
- documentation: {
- value: SuggestAdapter.createDocumentationString(details)
- }
- };
- }
- private static convertKind(kind: string): languages.CompletionItemKind {
- switch (kind) {
- case Kind.primitiveType:
- case Kind.keyword:
- return languages.CompletionItemKind.Keyword;
- case Kind.variable:
- case Kind.localVariable:
- return languages.CompletionItemKind.Variable;
- case Kind.memberVariable:
- case Kind.memberGetAccessor:
- case Kind.memberSetAccessor:
- return languages.CompletionItemKind.Field;
- case Kind.function:
- case Kind.memberFunction:
- case Kind.constructSignature:
- case Kind.callSignature:
- case Kind.indexSignature:
- return languages.CompletionItemKind.Function;
- case Kind.enum:
- return languages.CompletionItemKind.Enum;
- case Kind.module:
- return languages.CompletionItemKind.Module;
- case Kind.class:
- return languages.CompletionItemKind.Class;
- case Kind.interface:
- return languages.CompletionItemKind.Interface;
- case Kind.warning:
- return languages.CompletionItemKind.File;
- }
- return languages.CompletionItemKind.Property;
- }
- private static createDocumentationString(
- details: ts.CompletionEntryDetails
- ): string {
- let documentationString = displayPartsToString(details.documentation);
- if (details.tags) {
- for (const tag of details.tags) {
- documentationString += `\n\n${tagToString(tag)}`;
- }
- }
- return documentationString;
- }
- }
- function tagToString(tag: ts.JSDocTagInfo): string {
- let tagLabel = `*@${tag.name}*`;
- if (tag.name === 'param' && tag.text) {
- const [paramName, ...rest] = tag.text.split(' ');
- tagLabel += `\`${paramName}\``;
- if (rest.length > 0) tagLabel += ` — ${rest.join(' ')}`;
- } else if (tag.text) {
- tagLabel += ` — ${tag.text}`;
- }
- return tagLabel;
- }
- export class SignatureHelpAdapter
- extends Adapter
- implements languages.SignatureHelpProvider {
- public signatureHelpTriggerCharacters = ['(', ','];
- public async provideSignatureHelp(
- model: editor.ITextModel,
- position: Position,
- token: CancellationToken
- ): Promise<languages.SignatureHelpResult | undefined> {
- const resource = model.uri;
- const offset = model.getOffsetAt(position);
- const worker = await this._worker(resource);
- const info = await worker.getSignatureHelpItems(
- resource.toString(),
- offset
- );
- if (!info || model.isDisposed()) {
- return;
- }
- const ret: languages.SignatureHelp = {
- activeSignature: info.selectedItemIndex,
- activeParameter: info.argumentIndex,
- signatures: []
- };
- info.items.forEach((item) => {
- const signature: languages.SignatureInformation = {
- label: '',
- parameters: []
- };
- signature.documentation = {
- value: displayPartsToString(item.documentation)
- };
- signature.label += displayPartsToString(item.prefixDisplayParts);
- item.parameters.forEach((p, i, a) => {
- const label = displayPartsToString(p.displayParts);
- const parameter: languages.ParameterInformation = {
- label: label,
- documentation: {
- value: displayPartsToString(p.documentation)
- }
- };
- signature.label += label;
- signature.parameters.push(parameter);
- if (i < a.length - 1) {
- signature.label += displayPartsToString(item.separatorDisplayParts);
- }
- });
- signature.label += displayPartsToString(item.suffixDisplayParts);
- ret.signatures.push(signature);
- });
- return {
- value: ret,
- dispose() {}
- };
- }
- }
- // --- hover ------
- export class QuickInfoAdapter
- extends Adapter
- implements languages.HoverProvider {
- public async provideHover(
- model: editor.ITextModel,
- position: Position,
- token: CancellationToken
- ): Promise<languages.Hover | undefined> {
- const resource = model.uri;
- const offset = model.getOffsetAt(position);
- const worker = await this._worker(resource);
- const info = await worker.getQuickInfoAtPosition(
- resource.toString(),
- offset
- );
- if (!info || model.isDisposed()) {
- return;
- }
- const documentation = displayPartsToString(info.documentation);
- const tags = info.tags
- ? info.tags.map((tag) => tagToString(tag)).join(' \n\n')
- : '';
- const contents = displayPartsToString(info.displayParts);
- return {
- range: this._textSpanToRange(model, info.textSpan),
- contents: [
- {
- value: '```typescript\n' + contents + '\n```\n'
- },
- {
- value: documentation + (tags ? '\n\n' + tags : '')
- }
- ]
- };
- }
- }
- // --- occurrences ------
- export class OccurrencesAdapter
- extends Adapter
- implements languages.DocumentHighlightProvider {
- public async provideDocumentHighlights(
- model: editor.ITextModel,
- position: Position,
- token: CancellationToken
- ): Promise<languages.DocumentHighlight[] | undefined> {
- const resource = model.uri;
- const offset = model.getOffsetAt(position);
- const worker = await this._worker(resource);
- const entries = await worker.getOccurrencesAtPosition(
- resource.toString(),
- offset
- );
- if (!entries || model.isDisposed()) {
- return;
- }
- return entries.map((entry) => {
- return <languages.DocumentHighlight>{
- range: this._textSpanToRange(model, entry.textSpan),
- kind: entry.isWriteAccess
- ? languages.DocumentHighlightKind.Write
- : languages.DocumentHighlightKind.Text
- };
- });
- }
- }
- // --- definition ------
- export class DefinitionAdapter extends Adapter {
- constructor(
- private readonly _libFiles: LibFiles,
- worker: (...uris: Uri[]) => Promise<TypeScriptWorker>
- ) {
- super(worker);
- }
- public async provideDefinition(
- model: editor.ITextModel,
- position: Position,
- token: CancellationToken
- ): Promise<languages.Definition | undefined> {
- const resource = model.uri;
- const offset = model.getOffsetAt(position);
- const worker = await this._worker(resource);
- const entries = await worker.getDefinitionAtPosition(
- resource.toString(),
- offset
- );
- if (!entries || model.isDisposed()) {
- return;
- }
- // Fetch lib files if necessary
- await this._libFiles.fetchLibFilesIfNecessary(
- entries.map((entry) => Uri.parse(entry.fileName))
- );
- if (model.isDisposed()) {
- return;
- }
- const result: languages.Location[] = [];
- for (let entry of entries) {
- const uri = Uri.parse(entry.fileName);
- const refModel = this._libFiles.getOrCreateModel(uri);
- if (refModel) {
- result.push({
- uri: uri,
- range: this._textSpanToRange(refModel, entry.textSpan)
- });
- }
- }
- return result;
- }
- }
- // --- references ------
- export class ReferenceAdapter
- extends Adapter
- implements languages.ReferenceProvider {
- constructor(
- private readonly _libFiles: LibFiles,
- worker: (...uris: Uri[]) => Promise<TypeScriptWorker>
- ) {
- super(worker);
- }
- public async provideReferences(
- model: editor.ITextModel,
- position: Position,
- context: languages.ReferenceContext,
- token: CancellationToken
- ): Promise<languages.Location[] | undefined> {
- const resource = model.uri;
- const offset = model.getOffsetAt(position);
- const worker = await this._worker(resource);
- const entries = await worker.getReferencesAtPosition(
- resource.toString(),
- offset
- );
- if (!entries || model.isDisposed()) {
- return;
- }
- // Fetch lib files if necessary
- await this._libFiles.fetchLibFilesIfNecessary(
- entries.map((entry) => Uri.parse(entry.fileName))
- );
- if (model.isDisposed()) {
- return;
- }
- const result: languages.Location[] = [];
- for (let entry of entries) {
- const uri = Uri.parse(entry.fileName);
- const refModel = this._libFiles.getOrCreateModel(uri);
- if (refModel) {
- result.push({
- uri: uri,
- range: this._textSpanToRange(refModel, entry.textSpan)
- });
- }
- }
- return result;
- }
- }
- // --- outline ------
- export class OutlineAdapter
- extends Adapter
- implements languages.DocumentSymbolProvider {
- public async provideDocumentSymbols(
- model: editor.ITextModel,
- token: CancellationToken
- ): Promise<languages.DocumentSymbol[] | undefined> {
- const resource = model.uri;
- const worker = await this._worker(resource);
- const items = await worker.getNavigationBarItems(resource.toString());
- if (!items || model.isDisposed()) {
- return;
- }
- const convert = (
- bucket: languages.DocumentSymbol[],
- item: ts.NavigationBarItem,
- containerLabel?: string
- ): void => {
- let result: languages.DocumentSymbol = {
- name: item.text,
- detail: '',
- kind: <languages.SymbolKind>(
- (outlineTypeTable[item.kind] || languages.SymbolKind.Variable)
- ),
- range: this._textSpanToRange(model, item.spans[0]),
- selectionRange: this._textSpanToRange(model, item.spans[0]),
- tags: [],
- containerName: containerLabel
- };
- if (item.childItems && item.childItems.length > 0) {
- for (let child of item.childItems) {
- convert(bucket, child, result.name);
- }
- }
- bucket.push(result);
- };
- let result: languages.DocumentSymbol[] = [];
- items.forEach((item) => convert(result, item));
- return result;
- }
- }
- export class Kind {
- public static unknown: string = '';
- public static keyword: string = 'keyword';
- public static script: string = 'script';
- public static module: string = 'module';
- public static class: string = 'class';
- public static interface: string = 'interface';
- public static type: string = 'type';
- public static enum: string = 'enum';
- public static variable: string = 'var';
- public static localVariable: string = 'local var';
- public static function: string = 'function';
- public static localFunction: string = 'local function';
- public static memberFunction: string = 'method';
- public static memberGetAccessor: string = 'getter';
- public static memberSetAccessor: string = 'setter';
- public static memberVariable: string = 'property';
- public static constructorImplementation: string = 'constructor';
- public static callSignature: string = 'call';
- public static indexSignature: string = 'index';
- public static constructSignature: string = 'construct';
- public static parameter: string = 'parameter';
- public static typeParameter: string = 'type parameter';
- public static primitiveType: string = 'primitive type';
- public static label: string = 'label';
- public static alias: string = 'alias';
- public static const: string = 'const';
- public static let: string = 'let';
- public static warning: string = 'warning';
- }
- let outlineTypeTable: {
- [kind: string]: languages.SymbolKind;
- } = Object.create(null);
- outlineTypeTable[Kind.module] = languages.SymbolKind.Module;
- outlineTypeTable[Kind.class] = languages.SymbolKind.Class;
- outlineTypeTable[Kind.enum] = languages.SymbolKind.Enum;
- outlineTypeTable[Kind.interface] = languages.SymbolKind.Interface;
- outlineTypeTable[Kind.memberFunction] = languages.SymbolKind.Method;
- outlineTypeTable[Kind.memberVariable] = languages.SymbolKind.Property;
- outlineTypeTable[Kind.memberGetAccessor] = languages.SymbolKind.Property;
- outlineTypeTable[Kind.memberSetAccessor] = languages.SymbolKind.Property;
- outlineTypeTable[Kind.variable] = languages.SymbolKind.Variable;
- outlineTypeTable[Kind.const] = languages.SymbolKind.Variable;
- outlineTypeTable[Kind.localVariable] = languages.SymbolKind.Variable;
- outlineTypeTable[Kind.variable] = languages.SymbolKind.Variable;
- outlineTypeTable[Kind.function] = languages.SymbolKind.Function;
- outlineTypeTable[Kind.localFunction] = languages.SymbolKind.Function;
- // --- formatting ----
- export abstract class FormatHelper extends Adapter {
- protected static _convertOptions(
- options: languages.FormattingOptions
- ): ts.FormatCodeOptions {
- return {
- ConvertTabsToSpaces: options.insertSpaces,
- TabSize: options.tabSize,
- IndentSize: options.tabSize,
- IndentStyle: IndentStyle.Smart,
- NewLineCharacter: '\n',
- InsertSpaceAfterCommaDelimiter: true,
- InsertSpaceAfterSemicolonInForStatements: true,
- InsertSpaceBeforeAndAfterBinaryOperators: true,
- InsertSpaceAfterKeywordsInControlFlowStatements: true,
- InsertSpaceAfterFunctionKeywordForAnonymousFunctions: true,
- InsertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis: false,
- InsertSpaceAfterOpeningAndBeforeClosingNonemptyBrackets: false,
- InsertSpaceAfterOpeningAndBeforeClosingTemplateStringBraces: false,
- PlaceOpenBraceOnNewLineForControlBlocks: false,
- PlaceOpenBraceOnNewLineForFunctions: false
- };
- }
- protected _convertTextChanges(
- model: editor.ITextModel,
- change: ts.TextChange
- ): languages.TextEdit {
- return {
- text: change.newText,
- range: this._textSpanToRange(model, change.span)
- };
- }
- }
- export class FormatAdapter
- extends FormatHelper
- implements languages.DocumentRangeFormattingEditProvider {
- public async provideDocumentRangeFormattingEdits(
- model: editor.ITextModel,
- range: Range,
- options: languages.FormattingOptions,
- token: CancellationToken
- ): Promise<languages.TextEdit[] | undefined> {
- const resource = model.uri;
- const startOffset = model.getOffsetAt({
- lineNumber: range.startLineNumber,
- column: range.startColumn
- });
- const endOffset = model.getOffsetAt({
- lineNumber: range.endLineNumber,
- column: range.endColumn
- });
- const worker = await this._worker(resource);
- const edits = await worker.getFormattingEditsForRange(
- resource.toString(),
- startOffset,
- endOffset,
- FormatHelper._convertOptions(options)
- );
- if (!edits || model.isDisposed()) {
- return;
- }
- return edits.map((edit) => this._convertTextChanges(model, edit));
- }
- }
- export class FormatOnTypeAdapter
- extends FormatHelper
- implements languages.OnTypeFormattingEditProvider {
- get autoFormatTriggerCharacters() {
- return [';', '}', '\n'];
- }
- public async provideOnTypeFormattingEdits(
- model: editor.ITextModel,
- position: Position,
- ch: string,
- options: languages.FormattingOptions,
- token: CancellationToken
- ): Promise<languages.TextEdit[] | undefined> {
- const resource = model.uri;
- const offset = model.getOffsetAt(position);
- const worker = await this._worker(resource);
- const edits = await worker.getFormattingEditsAfterKeystroke(
- resource.toString(),
- offset,
- ch,
- FormatHelper._convertOptions(options)
- );
- if (!edits || model.isDisposed()) {
- return;
- }
- return edits.map((edit) => this._convertTextChanges(model, edit));
- }
- }
- // --- code actions ------
- export class CodeActionAdaptor
- extends FormatHelper
- implements languages.CodeActionProvider {
- public async provideCodeActions(
- model: editor.ITextModel,
- range: Range,
- context: languages.CodeActionContext,
- token: CancellationToken
- ): Promise<languages.CodeActionList> {
- const resource = model.uri;
- const start = model.getOffsetAt({
- lineNumber: range.startLineNumber,
- column: range.startColumn
- });
- const end = model.getOffsetAt({
- lineNumber: range.endLineNumber,
- column: range.endColumn
- });
- const formatOptions = FormatHelper._convertOptions(model.getOptions());
- const errorCodes = context.markers
- .filter((m) => m.code)
- .map((m) => m.code)
- .map(Number);
- const worker = await this._worker(resource);
- const codeFixes = await worker.getCodeFixesAtPosition(
- resource.toString(),
- start,
- end,
- errorCodes,
- formatOptions
- );
- if (!codeFixes || model.isDisposed()) {
- return { actions: [], dispose: () => {} };
- }
- const actions = codeFixes
- .filter((fix) => {
- // Removes any 'make a new file'-type code fix
- return fix.changes.filter((change) => change.isNewFile).length === 0;
- })
- .map((fix) => {
- return this._tsCodeFixActionToMonacoCodeAction(model, context, fix);
- });
- return {
- actions: actions,
- dispose: () => {}
- };
- }
- private _tsCodeFixActionToMonacoCodeAction(
- model: editor.ITextModel,
- context: languages.CodeActionContext,
- codeFix: ts.CodeFixAction
- ): languages.CodeAction {
- const edits: languages.WorkspaceTextEdit[] = [];
- for (const change of codeFix.changes) {
- for (const textChange of change.textChanges) {
- edits.push({
- resource: model.uri,
- edit: {
- range: this._textSpanToRange(model, textChange.span),
- text: textChange.newText
- }
- });
- }
- }
- const action: languages.CodeAction = {
- title: codeFix.description,
- edit: { edits: edits },
- diagnostics: context.markers,
- kind: 'quickfix'
- };
- return action;
- }
- }
- // --- rename ----
- export class RenameAdapter extends Adapter implements languages.RenameProvider {
- public async provideRenameEdits(
- model: editor.ITextModel,
- position: Position,
- newName: string,
- token: CancellationToken
- ): Promise<(languages.WorkspaceEdit & languages.Rejection) | undefined> {
- const resource = model.uri;
- const fileName = resource.toString();
- const offset = model.getOffsetAt(position);
- const worker = await this._worker(resource);
- const renameInfo = await worker.getRenameInfo(fileName, offset, {
- allowRenameOfImportPath: false
- });
- if (renameInfo.canRename === false) {
- // use explicit comparison so that the discriminated union gets resolved properly
- return {
- edits: [],
- rejectReason: renameInfo.localizedErrorMessage
- };
- }
- if (renameInfo.fileToRename !== undefined) {
- throw new Error('Renaming files is not supported.');
- }
- const renameLocations = await worker.findRenameLocations(
- fileName,
- offset,
- /*strings*/ false,
- /*comments*/ false,
- /*prefixAndSuffix*/ false
- );
- if (!renameLocations || model.isDisposed()) {
- return;
- }
- const edits: languages.WorkspaceTextEdit[] = [];
- for (const renameLocation of renameLocations) {
- edits.push({
- resource: Uri.parse(renameLocation.fileName),
- edit: {
- range: this._textSpanToRange(model, renameLocation.textSpan),
- text: newName
- }
- });
- }
- return { edits };
- }
- }
|