|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { CodeActionProvider, CodeAction, TextDocument, Range, Selection, CodeActionContext, CancellationToken, ProviderResult, Command, CodeActionKind, Diagnostic, WorkspaceEdit, EndOfLine, ExtensionContext, commands, CodeActionProviderMetadata, workspace, Uri, window, TextEditor } from "vscode"; |
| 4 | +import { Commands } from "../commands"; |
| 5 | + |
| 6 | +export class PomCodeActionProvider implements CodeActionProvider<CodeAction> { |
| 7 | + constructor(context: ExtensionContext) { |
| 8 | + context.subscriptions.push(commands.registerCommand("_java.projectConfiguration.saveAndUpdate", async (uri: Uri) => { |
| 9 | + const document: TextDocument = await workspace.openTextDocument(uri); |
| 10 | + await document.save(); |
| 11 | + commands.executeCommand(Commands.CONFIGURATION_UPDATE, uri); |
| 12 | + })); |
| 13 | + } |
| 14 | + |
| 15 | + provideCodeActions(document: TextDocument, range: Range | Selection, context: CodeActionContext, token: CancellationToken): ProviderResult<(Command | CodeAction)[]> { |
| 16 | + if (context?.diagnostics?.length && context.diagnostics[0].source === "Java") { |
| 17 | + return this.collectCodeActionsForNotCoveredExecutions(document, context.diagnostics); |
| 18 | + } |
| 19 | + |
| 20 | + return undefined; |
| 21 | + } |
| 22 | + |
| 23 | + private collectCodeActionsForNotCoveredExecutions(document: TextDocument, diagnostics: readonly Diagnostic[]): CodeAction[] { |
| 24 | + const codeActions: CodeAction[] = []; |
| 25 | + for (const diagnostic of diagnostics) { |
| 26 | + if (diagnostic.message?.startsWith("Plugin execution not covered by lifecycle configuration")) { |
| 27 | + const indentation = this.getNewTextIndentation(document, diagnostic); |
| 28 | + const saveAndUpdateConfigCommand: Command = { |
| 29 | + title: "Save and update project configuration", |
| 30 | + command: "_java.projectConfiguration.saveAndUpdate", |
| 31 | + arguments: [ document.uri ], |
| 32 | + }; |
| 33 | + |
| 34 | + const action1 = new CodeAction("Enable this execution in project configuration phase", CodeActionKind.QuickFix.append("pom")); |
| 35 | + action1.edit = new WorkspaceEdit(); |
| 36 | + action1.edit.insert(document.uri, diagnostic.range.end, indentation + "<?m2e execute onConfiguration?>"); |
| 37 | + action1.command = saveAndUpdateConfigCommand; |
| 38 | + codeActions.push(action1); |
| 39 | + |
| 40 | + const action2 = new CodeAction("Enable this execution in project build phase", CodeActionKind.QuickFix.append("pom")); |
| 41 | + action2.edit = new WorkspaceEdit(); |
| 42 | + action2.edit.insert(document.uri, diagnostic.range.end, indentation + "<?m2e execute onConfiguration,onIncremental?>"); |
| 43 | + action2.command = saveAndUpdateConfigCommand; |
| 44 | + codeActions.push(action2); |
| 45 | + |
| 46 | + const action3 = new CodeAction("Mark this execution as ignored in pom.xml", CodeActionKind.QuickFix.append("pom")); |
| 47 | + action3.edit = new WorkspaceEdit(); |
| 48 | + action3.edit.insert(document.uri, diagnostic.range.end, indentation + "<?m2e ignore?>"); |
| 49 | + action3.command = saveAndUpdateConfigCommand; |
| 50 | + codeActions.push(action3); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return codeActions; |
| 55 | + } |
| 56 | + |
| 57 | + private getNewTextIndentation(document: TextDocument, diagnostic: Diagnostic): string { |
| 58 | + const textline = document.lineAt(diagnostic.range.end.line); |
| 59 | + if (textline.text.lastIndexOf("</execution>") > diagnostic.range.end.character) { |
| 60 | + return ""; |
| 61 | + } |
| 62 | + |
| 63 | + let tabSize: number = 2; // default value |
| 64 | + let insertSpaces: boolean = true; // default value |
| 65 | + const activeEditor: TextEditor | undefined = window.activeTextEditor; |
| 66 | + if (activeEditor && activeEditor.document.uri.toString() === document.uri.toString()) { |
| 67 | + tabSize = Number(activeEditor.options.tabSize); |
| 68 | + insertSpaces = Boolean(activeEditor.options.insertSpaces); |
| 69 | + } |
| 70 | + |
| 71 | + const lineSeparator = document.eol === EndOfLine.LF ? "\r" : "\r\n"; |
| 72 | + let newIndentation = lineSeparator + textline.text.substring(0, textline.firstNonWhitespaceCharacterIndex); |
| 73 | + if (insertSpaces) { |
| 74 | + for (let i = 0; i < tabSize; i++) { |
| 75 | + newIndentation += ' '; // insert a space char. |
| 76 | + } |
| 77 | + } else { |
| 78 | + newIndentation += ' '; // insert a tab char. |
| 79 | + } |
| 80 | + |
| 81 | + return newIndentation; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export const pomCodeActionMetadata: CodeActionProviderMetadata = { |
| 86 | + providedCodeActionKinds: [ |
| 87 | + CodeActionKind.QuickFix.append("pom") |
| 88 | + ], |
| 89 | + documentation: [ |
| 90 | + { |
| 91 | + kind: CodeActionKind.QuickFix.append("pom"), |
| 92 | + command: { |
| 93 | + title: "Learn more about not covered Maven execution", |
| 94 | + command: Commands.NOT_COVERED_EXECUTION |
| 95 | + } |
| 96 | + } |
| 97 | + ], |
| 98 | +}; |
0 commit comments