Skip to content

Commit

Permalink
fix: code action not working with full document range
Browse files Browse the repository at this point in the history
close #726
  • Loading branch information
johnsoncodehk committed Nov 28, 2021
1 parent 388465f commit 7489313
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 3 deletions.
20 changes: 20 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ export function getWordRange(wordPattern: RegExp, position: vscode.Position, doc
return undefined;
}

export function getOverlapRange(range1: vscode.Range, range2: vscode.Range): vscode.Range | undefined {

const start: vscode.Position = {
line: Math.max(range1.start.line, range2.start.line),
character: range1.start.line === range2.start.line ? Math.max(range1.start.character, range2.start.character) : range1.start.line > range2.start.line ? range1.start.character : range2.start.character,
};
const end: vscode.Position = {
line: Math.min(range1.end.line, range2.end.line),
character: range1.end.line === range2.end.line ? Math.min(range1.end.character, range2.end.character) : range1.end.line < range2.end.line ? range1.end.character : range2.end.character,
};

if (start.line > end.line || (start.line === end.line && start.character > end.line))
return undefined;

return {
start,
end,
};
}

export function eqSet<T>(as: Set<T>, bs: Set<T>) {
if (as.size !== bs.size) return false;
for (const a of as) if (!bs.has(a)) return false;
Expand Down
34 changes: 34 additions & 0 deletions packages/vscode-vue-languageservice/src/services/codeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,45 @@ import type { ApiLanguageServiceContext } from '../types';
import * as dedupe from '../utils/dedupe';
import { tsEditToVueEdit } from './rename';
import type { Data } from './callHierarchy';
import * as shared from '@volar/shared';

export function register({ sourceFiles, getCssLs, getTsLs }: ApiLanguageServiceContext) {

return async (uri: string, range: vscode.Range, context: vscode.CodeActionContext) => {

const sourceFile = sourceFiles.get(uri);
if (sourceFile) {

const descriptor = sourceFile.getDescriptor();
const document = sourceFile.getTextDocument();

const scripts = [descriptor.script, descriptor.scriptSetup].filter(shared.notEmpty);
const styles = descriptor.styles;

const scriptRanges = scripts
.map(script => ({
start: document.positionAt(script.startTagEnd),
end: document.positionAt(script.startTagEnd + script.content.length),
}))
.map(scriptRange => shared.getOverlapRange(scriptRange, range))
.filter(shared.notEmpty);
const styleRanges = styles
.map(script => ({
start: document.positionAt(script.startTagEnd),
end: document.positionAt(script.startTagEnd + script.content.length),
}))
.map(scriptRange => shared.getOverlapRange(scriptRange, range))
.filter(shared.notEmpty);

const tsResult = (await Promise.all(scriptRanges.map(scriptRange => onTs(uri, scriptRange, context)))).flat();
const cssResult = (await Promise.all(styleRanges.map(styleRange => onCss(uri, styleRange, context)))).flat();

return dedupe.withCodeAction([
...tsResult,
...cssResult,
]);
}

const tsResult = await onTs(uri, range, context);
const cssResult = onCss(uri, range, context);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,8 @@ export function register({ sourceFiles, getCssLs, jsonLs, templateTsLs, scriptTs
cache: cacheWithSourceMap,
};
}
function toSourceDiags<T = vscode.Diagnostic | vscode.Diagnostic>(errors: T[], virtualScriptUri: string, sourceMaps: SourceMap[]) {
const result: T[] = [];
function toSourceDiags(errors: vscode.Diagnostic[], virtualScriptUri: string, sourceMaps: SourceMap[]) {
const result: vscode.Diagnostic[] = [];
for (const error of errors) {
if (vscode.Diagnostic.is(error)) {
for (const sourceMap of sourceMaps) {
Expand All @@ -610,7 +610,7 @@ export function register({ sourceFiles, getCssLs, jsonLs, templateTsLs, scriptTs
continue;
result.push({
...error,
range: vueRange,
range: vueRange[0],
});
}
}
Expand Down

0 comments on commit 7489313

Please sign in to comment.