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

Fix quick fix of placeholder/key #67

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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