diff --git a/CHANGELOG.md b/CHANGELOG.md index b1916a7..7fb8a2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how ## [0.1.0] - Fix [#62](https://github.com/google/arb-editor/issues/62). +- Fix quick fix of placeholder/key [#66](https://github.com/google/arb-editor/issues/66). ## [0.0.12] diff --git a/src/codeactions.ts b/src/codeactions.ts index dc55081..e295a4a 100644 --- a/src/codeactions.ts +++ b/src/codeactions.ts @@ -51,15 +51,19 @@ export class CodeActions implements vscode.CodeActionProvider { } private createPlaceholder(document: vscode.TextDocument, range: vscode.Range | vscode.Selection): vscode.CodeAction | undefined { - const placeholder = this.messageList?.getMessageAt(document.offsetAt(range.start)) as Placeholder | undefined; + const offset = document.offsetAt(range.start); + const placeholder = this.messageList + ?.getMessageAt(offset) + ?.getPlaceholders() + ?.find((p) => p.whereIs(offset) !== null); if (!placeholder) { return; } - var parent = placeholder?.parent; + let parent = placeholder.parent; while (!(parent instanceof MessageEntry)) { parent = parent?.parent; } - const fix = new vscode.CodeAction(`Add metadata for placeholder '${placeholder?.value}'`, vscode.CodeActionKind.QuickFix); + const fix = new vscode.CodeAction(`Add metadata for placeholder '${placeholder.value}'`, vscode.CodeActionKind.QuickFix); fix.edit = new vscode.WorkspaceEdit(); const parentKey = (parent as MessageEntry).key; @@ -71,16 +75,16 @@ export class CodeActions implements vscode.CodeActionProvider { fix.edit.insert( document.uri, document.positionAt(lastPlaceholderEnd!), - `,\n${this.messageList!.getIndent(3)}"${placeholder?.value}": {}` + `,\n${this.messageList!.getIndent(3)}"${placeholder.value}": {}` ); } else if (metadata.lastPlaceholderEnd) { fix.edit.insert( document.uri, document.positionAt(metadata.lastPlaceholderEnd), - `\n${this.messageList!.getIndent(3)}"${placeholder?.value}": {}\n${this.messageList!.getIndent(2)}` + `\n${this.messageList!.getIndent(3)}"${placeholder.value}": {}\n${this.messageList!.getIndent(2)}` ); } else { - const insertable = `\n${this.messageList!.getIndent(2)}"placeholders": {\n${this.messageList!.getIndent(3)}"${placeholder?.value}": {}\n${this.messageList!.getIndent(2)}}\n${this.messageList!.getIndent()}`; + const insertable = `\n${this.messageList!.getIndent(2)}"placeholders": {\n${this.messageList!.getIndent(3)}"${placeholder.value}": {}\n${this.messageList!.getIndent(2)}}\n${this.messageList!.getIndent()}`; fix.edit.insert(document.uri, document.positionAt(metadata.metadataEnd), insertable); } return fix; diff --git a/src/messageParser.ts b/src/messageParser.ts index d92e42b..645318f 100644 --- a/src/messageParser.ts +++ b/src/messageParser.ts @@ -245,7 +245,7 @@ export class MessageList { return this.indentationCharacter.repeat((this.indentationCount ?? 0) * (indentLevel ?? 1)); } - getMessageAt(offset: number): Message | Metadata | null { + getMessageAt(offset: number): Message | null { return [...this.messageEntries, ...this.metadataEntries] .flatMap((entry) => [entry.key, entry.message]) .map((message) => message.whereIs(offset)) @@ -304,7 +304,7 @@ export class Literal extends Message { }; whereIs(offset: number): Message | null { - if (this.start < offset && offset < this.end) { + if (this.start <= offset && offset <= this.end) { return this; } else { return null; @@ -350,7 +350,7 @@ export class CombinedMessage extends Message { } whereIs(offset: number): Message | null { - if (this.start < offset && offset < this.end) { + if (this.start <= offset && offset <= this.end) { return this.parts .map((part) => part.whereIs(offset)) .find((whereIs) => whereIs !== null) ?? this; @@ -381,7 +381,7 @@ export class ComplexMessage extends Message { } whereIs(offset: number): Message | null { - if (this.start < offset && offset < this.end) { + if (this.start <= offset && offset <= this.end) { return Array.from(this.messages.entries()) .flatMap(([literal, message]) => [literal, message]) .map((part) => part.whereIs(offset)) @@ -406,7 +406,7 @@ export class Placeholder extends Literal { } whereIs(offset: number): Message | null { - if (this.start < offset && offset < this.end) { + if (this.start <= offset && offset <= this.end) { return this; } else { return null; @@ -430,7 +430,7 @@ export class PlaceholderMetadata extends Message { }; whereIs(offset: number): Message | null { - if (this.start < offset && offset < this.end) { + if (this.start <= offset && offset <= this.end) { return this; } else { return null;