Skip to content

Commit

Permalink
Merge pull request #4126 from jlelong/jl_3995_NewDocumentCommand
Browse files Browse the repository at this point in the history
Add intellisense for macros defined by \NewDocumentCommand
  • Loading branch information
jlelong authored Jan 5, 2024
2 parents 69860d1 + 71a80e3 commit d60203c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/completion/completer/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,20 @@ 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 += '{}'
} else if (entry.content === 'o' || entry.content === 'O') {
args += '[]'
}
}
})
}

if (found && !defined.has(`${name}${args}`)) {
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/armory/intellisense/newdocumentcommand.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\documentclass[10pt]{article}
\usepackage{xparse}
\NewDocumentCommand\testNoArg{}{ABC}
\DeclareDocumentCommand\testA{m}{ABC #1}
\NewDocumentCommand{\testB}{O{}m}{ABC #2 #1}
\ProvideDocumentCommand{\testC}{m o O{} m r() m}{ABC}
\begin{document}

\end{document}
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 d60203c

Please sign in to comment.