Skip to content

Commit

Permalink
Fix vuejs#899
Browse files Browse the repository at this point in the history
  • Loading branch information
octref authored and cereschen committed Sep 10, 2020
1 parent 7a48595 commit 7b266ab
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### 0.27.0

- Add `foldingRange` support to support dynamic folding ranges such as `#region`. #899.
- Add setting `vetur.validation.interpolation` so interpolation diagnostics and `eslint-plugin-vue` diagnostics can be configed separately. #2131.
- Fix VLS crash for *.vue files in node_modules. #2006.
- Upgrade to TypeScript 4.0.2 and fix symbol outline issue. #1849.
Expand Down
5 changes: 3 additions & 2 deletions server/src/embeddedSupport/languageModes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
ColorInformation,
Color,
ColorPresentation,
Command,
CodeAction,
WorkspaceEdit
WorkspaceEdit,
FoldingRange
} from 'vscode-languageserver-types';

import { getLanguageModelCache, LanguageModelCache } from './languageModelCache';
Expand Down Expand Up @@ -69,6 +69,7 @@ export interface LanguageMode {
format?(document: TextDocument, range: Range, options: FormattingOptions): TextEdit[];
findDocumentColors?(document: TextDocument): ColorInformation[];
getColorPresentations?(document: TextDocument, color: Color, range: Range): ColorPresentation[];
getFoldingRanges?(document: TextDocument): FoldingRange[];

onDocumentChanged?(filePath: string): void;
onDocumentRemoved(document: TextDocument): void;
Expand Down
39 changes: 38 additions & 1 deletion server/src/modes/script/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {
MarkupContent,
CodeAction,
CodeActionKind,
WorkspaceEdit
WorkspaceEdit,
FoldingRangeKind
} from 'vscode-languageserver-types';
import { LanguageMode } from '../../embeddedSupport/languageModes';
import { VueDocumentRegions, LanguageRange } from '../../embeddedSupport/embeddedSupport';
Expand Down Expand Up @@ -469,6 +470,28 @@ export async function getJavascriptMode(
});
return referenceResults;
},
getFoldingRanges(doc) {
const { scriptDoc, service } = updateCurrentVueTextDocument(doc);
if (!languageServiceIncludesFile(service, doc.uri)) {
return [];
}

const fileFsPath = getFileFsPath(doc.uri);
const spans = service.getOutliningSpans(fileFsPath);

return spans.map(s => {
const range = convertRange(scriptDoc, s.textSpan);
const kind = getFoldingRangeKind(s);

return {
startLine: range.start.line,
startCharacter: range.start.character,
endLine: range.end.line,
endCharacter: range.end.character,
kind
};
});
},
getCodeActions(doc, range, _formatParams, context) {
const { scriptDoc, service } = updateCurrentVueTextDocument(doc);
const fileName = getFileFsPath(scriptDoc.uri);
Expand Down Expand Up @@ -799,3 +822,17 @@ function getFilterText(insertText: string | undefined): string | undefined {
// In all other cases, fallback to using the insertText
return insertText;
}

function getFoldingRangeKind(span: ts.OutliningSpan): FoldingRangeKind | undefined {
switch (span.kind) {
case 'comment':
return FoldingRangeKind.Comment;
case 'region':
return FoldingRangeKind.Region;
case 'imports':
return FoldingRangeKind.Imports;
case 'code':
default:
return undefined;
}
}
4 changes: 4 additions & 0 deletions server/src/modes/style/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ function getStyleMode(
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.findDocumentColors(embedded, stylesheets.refreshAndGet(embedded));
},
getFoldingRanges(document) {
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.getFoldingRanges(embedded);
},
getColorPresentations(document, color, range) {
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.getColorPresentations(embedded, stylesheets.refreshAndGet(embedded), color, range);
Expand Down
33 changes: 30 additions & 3 deletions server/src/services/vls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import {
CompletionParams,
CompletionTriggerKind,
ExecuteCommandParams,
ApplyWorkspaceEditRequest
ApplyWorkspaceEditRequest,
FoldingRangeParams
} from 'vscode-languageserver';
import {
ColorInformation,
Expand All @@ -38,7 +39,8 @@ import {
TextDocumentChangeEvent,
TextEdit,
ColorPresentation,
Range
Range,
FoldingRange
} from 'vscode-languageserver-types';

import { URI } from 'vscode-uri';
Expand All @@ -54,6 +56,7 @@ import { logger } from '../log';
import { getDefaultVLSConfig, VLSFullConfig, VLSConfig } from '../config';
import { LanguageId } from '../embeddedSupport/embeddedSupport';
import { APPLY_REFACTOR_COMMAND } from '../modes/script/javascript';
import { EndOfLine } from 'vscode';

export class VLS {
// @Todo: Remove this and DocumentContext
Expand Down Expand Up @@ -156,6 +159,7 @@ export class VLS {
this.lspConnection.onHover(this.onHover.bind(this));
this.lspConnection.onReferences(this.onReferences.bind(this));
this.lspConnection.onSignatureHelp(this.onSignatureHelp.bind(this));
this.lspConnection.onFoldingRanges(this.onFoldingRanges.bind(this));
this.lspConnection.onCodeAction(this.onCodeAction.bind(this));

this.lspConnection.onDocumentColor(this.onDocumentColors.bind(this));
Expand Down Expand Up @@ -459,6 +463,28 @@ export class VLS {
return NULL_SIGNATURE;
}

onFoldingRanges({ textDocument }: FoldingRangeParams): FoldingRange[] {
const doc = this.documentService.getDocument(textDocument.uri)!;
const lmrs = this.languageModes.getAllLanguageModeRangesInDocument(doc);

const result: FoldingRange[] = [];

lmrs.forEach(lmr => {
if (lmr.mode.getFoldingRanges) {
lmr.mode.getFoldingRanges(doc).forEach(r => result.push(r));
}

result.push({
startLine: lmr.start.line,
startCharacter: lmr.start.character,
endLine: lmr.end.line,
endCharacter: lmr.end.character
});
});

return result;
}

onCodeAction({ textDocument, range, context }: CodeActionParams) {
const doc = this.documentService.getDocument(textDocument.uri)!;
const mode = this.languageModes.getModeAtPosition(doc, range.start);
Expand Down Expand Up @@ -563,7 +589,8 @@ export class VLS {
colorProvider: true,
executeCommandProvider: {
commands: [APPLY_REFACTOR_COMMAND]
}
},
foldingRangeProvider: true
};
}
}
Expand Down

0 comments on commit 7b266ab

Please sign in to comment.