Skip to content

Commit

Permalink
Fix quick fix of placeholder/key (#67)
Browse files Browse the repository at this point in the history
* Fix quick fix of placeholder/key

* Add CHANGELOG
  • Loading branch information
kzrnm authored Sep 27, 2024
1 parent 6505618 commit 962ca41
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
16 changes: 10 additions & 6 deletions src/codeactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/messageParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 962ca41

Please sign in to comment.