diff --git a/services/web/frontend/js/features/source-editor/languages/latex/completions/doc-commands.ts b/services/web/frontend/js/features/source-editor/languages/latex/completions/doc-commands.ts index a4556f9d7a..f54edebbcd 100644 --- a/services/web/frontend/js/features/source-editor/languages/latex/completions/doc-commands.ts +++ b/services/web/frontend/js/features/source-editor/languages/latex/completions/doc-commands.ts @@ -2,6 +2,7 @@ import { applySnippet, extendOverUnpairedClosingBrace } from './apply' import { Completion, CompletionContext } from '@codemirror/autocomplete' import { documentCommands } from '../document-commands' import { Command } from '../../../utils/tree-operations/commands' +import { syntaxTree } from '@codemirror/language' const commandNameFromLabel = (label: string): string | undefined => label.match(/^\\\w+/)?.[0] @@ -36,9 +37,8 @@ export function customCommandCompletions( } const countCommandUsage = (context: CompletionContext) => { - const { doc } = context.state - - const excludeLineNumber = doc.lineAt(context.pos).number + const tree = syntaxTree(context.state) + const currentNode = tree.resolveInner(context.pos, -1) const result = new Map< string, @@ -51,7 +51,7 @@ const countCommandUsage = (context: CompletionContext) => { } for (const command of commandListProjection.items) { - if (command.line === excludeLineNumber) { + if (command.from === currentNode.from) { continue } const label = buildLabel(command) diff --git a/services/web/test/frontend/features/source-editor/components/codemirror-editor-autocomplete.spec.tsx b/services/web/test/frontend/features/source-editor/components/codemirror-editor-autocomplete.spec.tsx index 1159d61c48..784d0ae1a8 100644 --- a/services/web/test/frontend/features/source-editor/components/codemirror-editor-autocomplete.spec.tsx +++ b/services/web/test/frontend/features/source-editor/components/codemirror-editor-autocomplete.spec.tsx @@ -1064,4 +1064,23 @@ describe('autocomplete', { scrollBehavior: false }, function () { cy.findAllByRole('option').contains('sometext.txt').click() activeEditorLine().should('have.text', '\\input{sometext.txt}') }) + + it('excludes the current command from completions', function () { + const scope = mockScope(mockDocContent('')) + + cy.mount( + + + + + + ) + + cy.get('.cm-line').eq(21).type('\\fff \\ff') + + cy.findAllByRole('listbox').should('have.length', 1) + cy.findAllByRole('option').contains('\\fff').click() + + cy.get('.cm-line').eq(21).should('have.text', '\\fff \\fff') + }) })