Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CodeAction over command #1704

Merged
merged 3 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Load different `eslint-plugin-vue` rulesets depending on workspace vue version. #2015.
- Remove leading empty line in diagnostic errors. #2067.
- `"vetur.completion.tagCasing": "initial"` causes double tag completion. #2053
- 🙌 Use CodeAction over command. Thanks to contribution from [Matt Bierner](@mjbvz). #1704.

### 0.25.0 | 2020-07-22 | [VSIX](https://marketplace.visualstudio.com/_apis/public/gallery/publishers/octref/vsextensions/vetur/0.25.0/vspackage)

Expand Down
15 changes: 5 additions & 10 deletions client/vueMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,13 @@ export async function activate(context: vscode.ExtensionContext) {
)
);

context.subscriptions.push(
vscode.commands.registerCommand('vetur.applyWorkspaceEdits', (args: WorkspaceEdit) => {
const edit = client.protocol2CodeConverter.asWorkspaceEdit(args)!;
vscode.workspace.applyEdit(edit);
})
);

context.subscriptions.push(
vscode.commands.registerCommand('vetur.chooseTypeScriptRefactoring', (args: any) => {
client
.sendRequest<vscode.Command | undefined>('requestCodeActionEdits', args)
.then(command => command && vscode.commands.executeCommand(command.command, ...command.arguments!));
client.sendRequest<WorkspaceEdit | undefined>('requestCodeActionEdits', args).then(edits => {
if (edits) {
vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edits)!);
}
});
})
);

Expand Down
8 changes: 5 additions & 3 deletions server/src/embeddedSupport/languageModes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import {
ColorInformation,
Color,
ColorPresentation,
Command
Command,
CodeAction,
WorkspaceEdit
} from 'vscode-languageserver-types';

import { getLanguageModelCache, LanguageModelCache } from './languageModelCache';
Expand Down Expand Up @@ -53,8 +55,8 @@ export interface LanguageMode {
range: Range,
formatParams: FormattingOptions,
context: CodeActionContext
): Command[];
getRefactorEdits?(doc: TextDocument, args: RefactorAction): Command;
): CodeAction[];
getRefactorEdits?(doc: TextDocument, args: RefactorAction): WorkspaceEdit;
doComplete?(document: TextDocument, position: Position): CompletionList;
doResolve?(document: TextDocument, item: CompletionItem): CompletionItem;
doHover?(document: TextDocument, position: Position): Hover;
Expand Down
44 changes: 26 additions & 18 deletions server/src/modes/script/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import {
Position,
FormattingOptions,
DiagnosticTag,
MarkupContent
MarkupContent,
CodeAction,
CodeActionKind,
WorkspaceEdit
} from 'vscode-languageserver-types';
import { LanguageMode } from '../../embeddedSupport/languageModes';
import { VueDocumentRegions, LanguageRange } from '../../embeddedSupport/embeddedSupport';
Expand Down Expand Up @@ -435,7 +438,7 @@ export async function getJavascriptMode(

const formatSettings: ts.FormatCodeSettings = getFormatCodeSettings(config);

const result: Command[] = [];
const result: CodeAction[] = [];
const fixes = service.getCodeFixesAtPosition(
fileName,
start,
Expand All @@ -452,7 +455,7 @@ export async function getJavascriptMode(

return result;
},
getRefactorEdits(doc: TextDocument, args: RefactorAction) {
getRefactorEdits(doc: TextDocument, args: RefactorAction): WorkspaceEdit {
const { service } = updateCurrentVueTextDocument(doc);
const response = service.getEditsForRefactor(
args.fileName,
Expand All @@ -464,10 +467,9 @@ export async function getJavascriptMode(
);
if (!response) {
// TODO: What happens when there's no response?
return createApplyCodeActionCommand('', {});
return {};
}
const uriMapping = createUriMappingForEdits(response.edits, service);
return createApplyCodeActionCommand('', uriMapping);
return { changes: createUriMappingForEdits(response.edits, service) };
},
format(doc: TextDocument, range: Range, formatParams: FormattingOptions): TextEdit[] {
const { scriptDoc, service } = updateCurrentVueTextDocument(doc);
Expand Down Expand Up @@ -551,7 +553,7 @@ function collectRefactoringCommands(
fileName: string,
formatSettings: any,
textRange: { pos: number; end: number },
result: Command[]
result: CodeAction[]
) {
const actions: RefactorAction[] = [];
for (const refactoring of refactorings) {
Expand Down Expand Up @@ -582,33 +584,39 @@ function collectRefactoringCommands(
}
for (const action of actions) {
result.push({
command: 'vetur.chooseTypeScriptRefactoring',
title: action.description,
arguments: [action]
kind: CodeActionKind.Refactor,
command: {
title: action.description,
command: 'vetur.chooseTypeScriptRefactoring',
arguments: [action]
}
});
}
}

function collectQuickFixCommands(
fixes: ReadonlyArray<ts.CodeFixAction>,
service: ts.LanguageService,
result: Command[]
result: CodeAction[]
) {
for (const fix of fixes) {
const uriTextEditMapping = createUriMappingForEdits(fix.changes, service);
result.push(createApplyCodeActionCommand(fix.description, uriTextEditMapping));
result.push(createApplyCodeAction(CodeActionKind.QuickFix, fix.description, uriTextEditMapping));
}
}

function createApplyCodeActionCommand(title: string, uriTextEditMapping: Record<string, TextEdit[]>): Command {
function createApplyCodeAction(
kind: CodeActionKind,
title: string,
uriTextEditMapping: Record<string, TextEdit[]>
): CodeAction {
return {
title,
command: 'vetur.applyWorkspaceEdits',
arguments: [
{
changes: uriTextEditMapping
}
]
kind,
edit: {
changes: uriTextEditMapping
}
};
}

Expand Down
5 changes: 1 addition & 4 deletions server/src/modes/style/stylus/completion-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ export function isValue(data: LoadedCSSData, currentWord: string): boolean {
* @return {String}
*/
export function getPropertyName(currentWord: string): string {
return currentWord
.trim()
.replace(':', ' ')
.split(' ')[0];
return currentWord.trim().replace(':', ' ').split(' ')[0];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions server/src/modes/template/tagProviders/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export enum Priority {
UserCode,
Library,
Framework,
Platform,
Platform
}

export interface IHTMLTagProvider {
Expand Down Expand Up @@ -79,7 +79,7 @@ export function collectAttributesDefault(
}
}
}
globalAttributes.forEach((attr) => {
globalAttributes.forEach(attr => {
collector(attr.label, attr.type, attr.documentation);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
collectValuesDefault,
genAttribute,
Priority,
Attribute,
Attribute
} from './common';
import { ChildComponent } from '../../../services/vueInfoService';
import { MarkupContent } from 'vscode-languageserver-types';
Expand All @@ -18,14 +18,14 @@ export function getComponentInfoTagProvider(childComponents: ChildComponent[]):
for (const cc of childComponents) {
const props: Attribute[] = [];
if (cc.info && cc.info.componentInfo.props) {
cc.info.componentInfo.props.forEach((p) => {
cc.info.componentInfo.props.forEach(p => {
props.push(genAttribute(p.name, undefined, { kind: 'markdown', value: p.documentation || '' }));
});
}
tagSet[cc.name] = new HTMLTagSpecification(
{
kind: 'markdown',
value: cc.documentation || '',
value: cc.documentation || ''
},
props
);
Expand All @@ -34,7 +34,7 @@ export function getComponentInfoTagProvider(childComponents: ChildComponent[]):
return {
getId: () => 'component',
priority: Priority.UserCode,
collectTags: (collector) => collectTagsDefault(collector, tagSet),
collectTags: collector => collectTagsDefault(collector, tagSet),
collectAttributes: (
tag: string,
collector: (attribute: string, type?: string, documentation?: string | MarkupContent) => void
Expand All @@ -43,6 +43,6 @@ export function getComponentInfoTagProvider(childComponents: ChildComponent[]):
},
collectValues: (tag: string, attribute: string, collector: (value: string) => void) => {
collectValuesDefault(tag, attribute, collector, tagSet, [], {});
},
}
};
}
Loading