Explorar el Código

Only exclude the current command from autocompletion (#14367)

GitOrigin-RevId: 1f1dc0f2c3bcd19b12e06ec56d2d82181b0b5c6d
Alf Eaton hace 3 años
padre
commit
06aa92d0fd

+ 4 - 4
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)

+ 19 - 0
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(
+      <Container>
+        <EditorProviders scope={scope}>
+          <CodeMirrorEditor />
+        </EditorProviders>
+      </Container>
+    )
+
+    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')
+  })
 })