import * as vscode from 'vscode' ; import { macrosOf } from './macros' ; import { Diagram } from '../diagram/diagram' ; import { diagramAt } from '../diagram/tools' ; import { dicLanguageWords } from './languageCompletion' ; const REG_VAR = /[0-9a-z_]+/ig ; const REG_EXCLUDE_LINE = /^\s*(!|@)/i ; export async function VariableCompletionItems ( target , position , token ) { if ( ! target ) return [ ] ; return new Promise < vscode . CompletionItem [ ] > ( ( resolve , reject ) => { let results = collectVariables ( target , position , token ) . map ( variable => { const item = new vscode . CompletionItem ( variable , vscode . CompletionItemKind . Variable ) ; item . insertText = new vscode . SnippetString ( variable ) ; return item ; } ) ; resolve ( results ) ; } ) ; } function collectVariables ( target , position , token ) { let variables = new Set < string > ( [ ] ) ; let diagram = target instanceof Diagram ? target : diagramAt ( target , position ) ; let excludeLine = position . line - diagram . start . line ; for ( let i = 0 ; i < diagram . lines . length ; i ++ ) { if ( i == excludeLine ) continue ; let line = diagram . lines [ i ] ; if ( REG_EXCLUDE_LINE . test ( line ) ) continue ; let matches ; REG_VAR . lastIndex = 0 ; while ( matches = REG_VAR . exec ( line ) ) { if ( ! dicLanguageWords . has ( matches [ 0 ] ) ) variables . add ( matches [ 0 ] ) ; } } return Array . from ( variables ) ; }