Skip to content

Commit

Permalink
Add quick fix to remove trailing separation
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Dec 30, 2024
1 parent c0ff05a commit 65a217a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
19 changes: 17 additions & 2 deletions packages/core/src/parser/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { localeQuote, localize } from '@spyglassmc/locales'
import type { AstNode, ListNode } from '../node/index.js'
import type { ParserContext } from '../service/index.js'
import type { Source } from '../source/index.js'
import { Range } from '../source/index.js'
import { ErrorSeverity, Range } from '../source/index.js'
import type { InfallibleParser, Parser } from './Parser.js'
import { Failure } from './Parser.js'
import { attempt } from './util.js'
Expand Down Expand Up @@ -73,9 +73,24 @@ export function list<V extends AstNode>(

// Trailing item sep.
if (hasValueSep && !trailingSep) {
const trailingRange = ans.children[ans.children.length - 1].sep!
ctx.err.report(
localize('parser.list.trailing-sep'),
ans.children[ans.children.length - 1].sep!,
trailingRange,
ErrorSeverity.Error,
{
codeAction: {
title: localize('code-action.remove-trailing-separation'),
isPreferred: true,
changes: [
{
type: 'edit',
range: trailingRange,
text: '',
},
],
},
},
)
}

Expand Down
19 changes: 17 additions & 2 deletions packages/core/src/parser/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AstNode } from '../node/index.js'
import type { RecordNode } from '../node/RecordNode.js'
import type { ParserContext } from '../service/index.js'
import type { Source } from '../source/index.js'
import { Range } from '../source/index.js'
import { ErrorSeverity, Range } from '../source/index.js'
import type { InfallibleParser, Parser } from './Parser.js'
import { Failure } from './Parser.js'
import { attempt } from './util.js'
Expand Down Expand Up @@ -127,9 +127,24 @@ export function record<K extends AstNode, V extends AstNode>(

// Trailing pair end.
if (hasPairEnd && !pair.trailingEnd) {
const trailingRange = ans.children[ans.children.length - 1].end!
ctx.err.report(
localize('parser.record.trailing-end'),
ans.children[ans.children.length - 1].end!,
trailingRange,
ErrorSeverity.Error,
{
codeAction: {
title: localize('code-action.remove-trailing-separation'),
isPreferred: true,
changes: [
{
type: 'edit',
range: trailingRange,
text: '',
},
],
},
},
)
}

Expand Down
1 change: 1 addition & 0 deletions packages/locales/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"code-action.nbt-type-to-long": "Convert to an NBT long tag",
"code-action.nbt-type-to-short": "Convert to an NBT short tag",
"code-action.nbt-uuid-datafix": "Update this UUID to 1.16",
"code-action.remove-trailing-separation": "Remove trailing separation",
"code-action.selector-sort-keys": "Sort selector argument",
"code-action.string-double-quote": "Quote this string with double quotation marks",
"code-action.string-single-quote": "Quote this string with single quotation marks",
Expand Down

0 comments on commit 65a217a

Please sign in to comment.