Skip to content

Commit

Permalink
fix(snippets): check inner placeholder first
Browse files Browse the repository at this point in the history
  • Loading branch information
chemzqm committed Mar 6, 2025
1 parent ea4d964 commit 130e60f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/__tests__/snippets/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ describe('SnippetParser', () => {
test('Parser, TM text', () => {
assertTextAndMarker('foo${1:bar}}', 'foobar}', Text, Placeholder, Text)
assertTextAndMarker('foo${1:bar}${2:foo}}', 'foobarfoo}', Text, Placeholder, Placeholder, Text)

assertTextAndMarker('foo${1:bar\\}${2:foo}}', 'foobar}foo', Text, Placeholder)

let [, placeholder] = new SnippetParser().parse('foo${1:bar\\}${2:foo}}').children
Expand Down Expand Up @@ -1102,6 +1101,12 @@ describe('SnippetParser', () => {
assertMarker(snippet, Placeholder)
})

test('Placeholder nestedPlaceholderCount', function() {
let { children } = new SnippetParser().parse('${1:foo${2:bar}}')
let placeholder = children[0] as Placeholder
assert.equal(placeholder.nestedPlaceholderCount, 1)
})

test('snippets variable not resolved in JSON proposal #52931', function() {
assertTextAndMarker('FOO${1:/bin/bash}', 'FOO/bin/bash', Text, Placeholder)
})
Expand Down
10 changes: 8 additions & 2 deletions src/__tests__/snippets/snippet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,14 @@ describe('CocSnippet', () => {
})

it('should compares placeholders', () => {
expect(comparePlaceholder({ primary: false, index: 1 }, { primary: false, index: 0 })).toBe(-1)
expect(comparePlaceholder({ primary: true, index: 1 }, { primary: false, index: 1 })).toBe(-1)
let arr = [
{ primary: false, index: 1, nestCount: 2 },
{ primary: true, index: 2, nestCount: 1 },
{ primary: false, index: 3, nestCount: 0 },
]
arr.sort(comparePlaceholder)
let indexes = arr.map(p => p.index)
expect(indexes).toEqual([3, 2, 1])
})
})

Expand Down
7 changes: 7 additions & 0 deletions src/snippets/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,13 @@ export class Placeholder extends TransformableMarker {
: undefined
}

public get nestedPlaceholderCount(): number {
if (this.transform) return 0
return this._children.reduce((p, marker) => {
return p + (marker instanceof Placeholder ? 1 + marker.nestedPlaceholderCount : 0)
}, 0)
}

public toTextmateString(): string {
let transformString = ''
if (this.transform) {
Expand Down
8 changes: 7 additions & 1 deletion src/snippets/snippet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface CocSnippetPlaceholder {
before: string
// snippet text after
after: string
nestCount: number
}

export class CocSnippet {
Expand Down Expand Up @@ -261,6 +262,7 @@ export class CocSnippet {
character: position.line == 0 ? character + position.character : position.character
}
let index: number
let nestCount = 0
if (p instanceof Snippets.Variable) {
let key = p.name
if (variableIndexMap.has(key)) {
Expand All @@ -272,12 +274,14 @@ export class CocSnippet {
}
} else {
index = p.index
nestCount = p.nestedPlaceholderCount
}
const value = p.toString()
const end = getEnd(position, value)
let res: CocSnippetPlaceholder = {
index,
value,
nestCount,
marker: p,
transform: !!p.transform,
range: Range.create(start, getEnd(start, value)),
Expand Down Expand Up @@ -459,7 +463,9 @@ export function shouldFormat(snippet: string): boolean {
return false
}

export function comparePlaceholder(a: { primary: boolean, index: number }, b: { primary: boolean, index: number }): number {
export function comparePlaceholder(a: { primary: boolean, index: number, nestCount: number }, b: { primary: boolean, index: number, nestCount: number }): number {
// check inner placeholder first
if (a.nestCount !== b.nestCount) return a.nestCount - b.nestCount
if (a.primary !== b.primary) return a.primary ? -1 : 1
if (a.index == 0 || b.index == 0) return a.index == 0 ? 1 : -1
return a.index - b.index
Expand Down

0 comments on commit 130e60f

Please sign in to comment.