Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Mar 7, 2024
1 parent 505a123 commit 08f78cb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
40 changes: 36 additions & 4 deletions packages/language-core/lib/virtualFile/computedMappings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Mapping, Segment, replaceSourceRange } from '@volar/language-core';
import { CodeMapping, Segment, replaceSourceRange } from '@volar/language-core';
import { computed } from 'computeds';
import type * as ts from 'typescript';
import { enableAllFeatures } from '../generators/utils';
import { disableAllFeatures, enableAllFeatures } from '../generators/utils';
import type { Sfc, VueCodeInformation } from '../types';

export function computedMappings(
Expand All @@ -21,9 +21,9 @@ export function computedMappings(
replaceSourceRange(str, undefined, block.startTagEnd, block.endTagStart, '\n\n');
}
}
return str
const mappings = str
.filter(s => typeof s !== 'string')
.map<Mapping<VueCodeInformation>>((m) => {
.map<CodeMapping>((m) => {
const text = m[0];
const start = m[2] as number;
return {
Expand All @@ -33,5 +33,37 @@ export function computedMappings(
data: m[3] as VueCodeInformation,
};
});

// fix folding range end position failed to mapping
for (const block of [
sfc.script,
sfc.scriptSetup,
sfc.template,
...sfc.styles,
...sfc.customBlocks,
]) {
const offsets: number[] = [];
if (block) {
let content = block.content;
if (content.endsWith('\r\n')) {
content = content.slice(0, -2);
}
else if (content.endsWith('\n')) {
content = content.slice(0, -1);
}
const offset = content.lastIndexOf('\n') + 1;
offsets.push(block.startTagEnd + offset);
}
if (offsets.length) {
mappings.push({
sourceOffsets: offsets,
generatedOffsets: offsets,
lengths: offsets.map(() => 0),
data: disableAllFeatures({ structure: true }),
});
}
}

return mappings;
});
}
24 changes: 0 additions & 24 deletions packages/language-service/lib/plugins/vue-sfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,6 @@ export function create(): ServicePlugin {
},
},

provideFoldingRanges(document) {
return worker(document, (vueCode) => {
const blocks = [
vueCode.sfc.script,
vueCode.sfc.scriptSetup,
vueCode.sfc.template,
...vueCode.sfc.styles,
...vueCode.sfc.customBlocks,
];
const result: vscode.FoldingRange[] = [];

for (const block of blocks) {
if (block) {
result.push({
startLine: document.positionAt(block.start).line,
endLine: document.positionAt(block.end).line,
});
}
}

return result;
});
},

async resolveEmbeddedCodeFormattingOptions(code, options) {

const sourceFile = context.language.files.getByVirtualCode(code);
Expand Down

0 comments on commit 08f78cb

Please sign in to comment.