Skip to content

Commit

Permalink
feat: redo on type format support
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Feb 17, 2023
1 parent 0a3c75f commit ec41473
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
31 changes: 24 additions & 7 deletions packages/language-service/src/documentFeatures/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,16 @@ export function register(context: LanguageServicePluginContext) {
start: map.virtualFileDocument.positionAt(0),
end: map.virtualFileDocument.positionAt(map.virtualFileDocument.getText().length),
});

if (virtualCodeEdits) {
toPatchIndentUris.push({
uri: map.virtualFileDocument.uri,
isCodeBlock,
});
}
}

if (!virtualCodeEdits)
continue;

toPatchIndentUris.push({
uri: map.virtualFileDocument.uri,
isCodeBlock,
});

for (const textEdit of virtualCodeEdits) {
const range = map.toSourceRange(textEdit.range);
if (range) {
Expand All @@ -118,6 +116,15 @@ export function register(context: LanguageServicePluginContext) {
if (level > 1) {

const baseIndent = options.insertSpaces ? ' '.repeat(options.tabSize) : '\t';
const editLines = new Set<number>();

if (onTypeParams) {
for (const edit of edits) {
for (let line = edit.range.start.line; line <= edit.range.end.line; line++) {
editLines.add(line);
}
}
}

for (const toPatchIndentUri of toPatchIndentUris) {

Expand All @@ -130,6 +137,16 @@ export function register(context: LanguageServicePluginContext) {
initialIndentLanguageId[map.virtualFileDocument.languageId] ? baseIndent : '',
);

if (onTypeParams) {
indentEdits = indentEdits.filter(edit => {
for (let line = edit.range.start.line; line <= edit.range.end.line; line++) {
if (!editLines.has(line))
return false;
}
return true;
});
}

indentEdits = indentEdits.filter(edit => isInsideRange(range!, edit.range));

if (indentEdits.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { languageFeatureWorker } from '../utils/featureWorkers';

export function register(context: LanguageServicePluginContext) {

return async (uri: string, position: vscode.Position) => {
return (uri: string, position: vscode.Position) => {

return languageFeatureWorker(
context,
Expand Down

0 comments on commit ec41473

Please sign in to comment.