Skip to content

Commit

Permalink
Add intellisense for macros defined by \NewDocumentCommand
Browse files Browse the repository at this point in the history
This command is provided by xparse
  • Loading branch information
jlelong committed Jan 5, 2024
1 parent 39ea1ba commit 00a8195
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/completion/completer/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,21 @@ function parseAst(node: Ast.Node, filePath: string, defined?: Set<string>): CmdE
parseInt(node.args?.[2].content?.[0].content) > 0) {
args = (node.args?.[3].openMark === '[' ? '[]' : '{}') + '{}'.repeat(parseInt(node.args?.[2].content?.[0].content) - 1)
}
} else if (node.type === 'macro' &&
['ReNewDocumentCommand', 'NewDocumentCommand', 'ProvideDocumentCommand', 'DeclareDocumentCommand'].includes(node.content) &&
node.args?.length === 3 && node.args[0]?.content?.[0]?.type === 'macro') {
found = true
name = node.args[0].content[0].content
node.args[1].content.forEach((entry: Ast.Node) => {
if (entry.type === 'string') {
if (entry.content === 'm') {
args += '{}'
}
if (entry.content === 'o' || entry.content === 'O') {
args += '[]'
}
}
})
}

if (found && !defined.has(`${name}${args}`)) {
Expand Down
11 changes: 11 additions & 0 deletions test/suites/04_intellisense.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ suite('Intellisense test suite', () => {
assert.ok(suggestions.labels.includes('\\fix[]{}{}'))
})

test.run('command intellisense with cmds defined by \\NewDocumentCommand', async (fixture: string) => {
await test.load(fixture, [
{src: 'intellisense/newdocumentcommand.tex', dst: 'main.tex'}
])
const suggestions = test.suggest(0, 1)
assert.ok(suggestions.labels.includes('\\testNoArg'))
assert.ok(suggestions.labels.includes('\\testA{}'))
assert.ok(suggestions.labels.includes('\\testB[]{}'))
assert.ok(suggestions.labels.includes('\\testC{}[][]{}{}'))
})

test.run('command intellisense with config `intellisense.argumentHint.enabled`', async (fixture: string) => {
await vscode.workspace.getConfiguration('latex-workshop').update('intellisense.argumentHint.enabled', true)
await test.load(fixture, [
Expand Down

0 comments on commit 00a8195

Please sign in to comment.