Skip to content

Commit

Permalink
feat: range formatting
Browse files Browse the repository at this point in the history
close #1370
  • Loading branch information
johnsoncodehk committed Jun 2, 2022
1 parent 271a587 commit 4e824dc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions packages/vue-language-server/src/features/documentFeatures.ts
Expand Up @@ -13,6 +13,11 @@ export function register(
return vueDs.format(document, handler.options);
});
});
connection.onDocumentRangeFormatting(handler => {
return worker(handler.textDocument.uri, document => {
return vueDs.format(document, handler.options, handler.range);
});
});
connection.onSelectionRanges(handler => {
return worker(handler.textDocument.uri, document => {
return vueDs.getSelectionRanges(document, handler.positions);
Expand Down
Expand Up @@ -22,5 +22,6 @@ export function register(
}
if (features.documentFormatting) {
server.documentFormattingProvider = true;
server.documentRangeFormattingProvider = true;
}
}
45 changes: 37 additions & 8 deletions packages/vue-language-service/src/documentFeatures/format.ts
Expand Up @@ -6,10 +6,14 @@ import { EmbeddedDocumentSourceMap, VueDocument } from '../vueDocuments';

export function register(context: DocumentServiceRuntimeContext) {

return async (document: TextDocument, options: vscode.FormattingOptions) => {
return async (document: TextDocument, options: vscode.FormattingOptions, range?: vscode.Range) => {

if (!range) {
range = vscode.Range.create(document.positionAt(0), document.positionAt(document.getText().length));
}

const originalDocument = document;
const rootEdits = await tryFormat(document);
const rootEdits = await tryFormat(document, range);
const vueDocument = context.getVueDocument(document);

if (!vueDocument)
Expand Down Expand Up @@ -42,12 +46,41 @@ export function register(context: DocumentServiceRuntimeContext) {

const sourceMap = vueDocument.sourceMapsMap.get(embedded.self);

let embeddedRange = sourceMap.getMappedRange(range.start, range.end)?.[0];

if (!embeddedRange) {

let start = sourceMap.getMappedRange(range.start)?.[0].start;
let end = sourceMap.getMappedRange(range.end)?.[0].end;

if (!start) {
const minSourceStart = Math.min(...sourceMap.mappings.map(m => m.sourceRange.start));
if (document.offsetAt(range.start) <= minSourceStart) {
start = range.start;
}
}

if (!end) {
const maxSourceEnd = Math.max(...sourceMap.mappings.map(m => m.sourceRange.end));
if (document.offsetAt(range.end) >= maxSourceEnd) {
end = range.end;
}
}

if (start && end) {
embeddedRange = { start, end };
}
}

if (!embeddedRange)
continue;

if (embedded.inheritParentIndent)
toPatchIndent = {
sourceMapEmbeddedDocumentUri: sourceMap.mappedDocument.uri,
};

const _edits = await tryFormat(sourceMap.mappedDocument);
const _edits = await tryFormat(sourceMap.mappedDocument, embeddedRange);

if (!_edits)
continue;
Expand Down Expand Up @@ -124,13 +157,9 @@ export function register(context: DocumentServiceRuntimeContext) {
}
}

async function tryFormat(document: TextDocument) {
async function tryFormat(document: TextDocument, range: vscode.Range) {

const plugins = context.getFormatPlugins();
const range: vscode.Range = {
start: document.positionAt(0),
end: document.positionAt(document.getText().length),
};

context.updateTsLs(document);

Expand Down

0 comments on commit 4e824dc

Please sign in to comment.