@@ -16,52 +16,61 @@ export function initCppCompletion(monaco: typeof import('monaco-editor')) {
1616 provideCompletionItems : ( model , position ) => {
1717 const word = model . getWordUntilPosition ( position ) ;
1818 const range = getRange ( position , word ) ;
19-
19+
2020 const code = model . getValue ( ) ;
21- const variables = extractVariables ( code , position ) ; // 传入当前光标位置,获取局部变量和全局变量
22-
23- const variableSuggestions : { label : string ; kind : languages . CompletionItemKind ; insertText : string ; documentation : string ; range : { startLineNumber : any ; endLineNumber : any ; startColumn : any ; endColumn : any ; } ; } [ ] = [ ] ;
24- variables . forEach ( ( type , name ) => {
21+ const variables = extractVariables ( code , position ) ;
22+
23+ const variableSuggestions : { label : string ; kind : languages . CompletionItemKind ; insertText : string ; documentation : string ; range : any ; } [ ] = [ ] ;
24+ variables . forEach ( ( { type } , name ) => {
2525 variableSuggestions . push ( {
26- label : name , // 只提示变量名,而不是完整声明
26+ label : name ,
2727 kind : monaco . languages . CompletionItemKind . Variable ,
2828 insertText : name ,
2929 documentation : `Variable of type ${ type } ` ,
3030 range : range
3131 } ) ;
3232 } ) ;
33-
33+
3434 const lineContent = model . getLineContent ( position . lineNumber ) ;
3535 const textBeforeCursor = lineContent . substring ( 0 , position . column - 1 ) . trim ( ) ;
3636 const lastDotIndex = textBeforeCursor . lastIndexOf ( '.' ) ;
37- let functionSuggestions : ( { label : string ; kind : languages . CompletionItemKind ; insertText : string ; insertTextRules : languages . CompletionItemInsertTextRule ; documentation : string ; range : any ; } | { label : string ; kind : languages . CompletionItemKind ; insertText : string ; documentation : string ; range : any ; insertTextRules ?: undefined ; } ) [ ] = [ ] ;
37+ let functionSuggestions : { label : string ; kind : languages . CompletionItemKind ; insertText : string ; documentation : string ; range : any ; } [ ] = [ ] ;
3838 let isDotAfterVariable = false ;
39-
39+
4040 if ( lastDotIndex !== - 1 ) {
4141 const varName = textBeforeCursor . substring ( 0 , lastDotIndex ) . trim ( ) ;
4242 if ( variables . has ( varName ) ) {
43- const varType = variables . get ( varName ) ;
43+ const varInfo = variables . get ( varName ) ;
4444 isDotAfterVariable = true ;
45-
46- if ( varType ?. startsWith ( 'vector' ) ) {
47- functionSuggestions = getVectorSuggestions ( monaco , range ) ;
48- } else if ( varType ?. startsWith ( 'stack' ) ) {
49- functionSuggestions = getStackSuggestions ( monaco , range ) ;
50- } else if ( varType ?. startsWith ( 'queue' ) ) {
51- functionSuggestions = getQueueSuggestions ( monaco , range ) ;
52- } else if ( varType ?. startsWith ( 'deque' ) ) {
53- functionSuggestions = getDequeSuggestions ( monaco , range ) ;
54- } else if ( varType ?. startsWith ( 'map' ) ) {
55- functionSuggestions = getMapSuggestions ( monaco , range ) ;
56- } else if ( varType ?. startsWith ( 'unordered_map' ) ) {
57- functionSuggestions = getUnorderedMapSuggestions ( monaco , range ) ;
45+
46+ // 根据变量类型返回相应的成员方法提示
47+ switch ( varInfo . type ) {
48+ case 'template' :
49+ functionSuggestions = getVectorSuggestions ( monaco , range ) ;
50+ break ;
51+ case 'stack' :
52+ functionSuggestions = getStackSuggestions ( monaco , range ) ;
53+ break ;
54+ case 'queue' :
55+ functionSuggestions = getQueueSuggestions ( monaco , range ) ;
56+ break ;
57+ case 'deque' :
58+ functionSuggestions = getDequeSuggestions ( monaco , range ) ;
59+ break ;
60+ case 'map' :
61+ functionSuggestions = getMapSuggestions ( monaco , range ) ;
62+ break ;
63+ case 'unordered_map' :
64+ functionSuggestions = getUnorderedMapSuggestions ( monaco , range ) ;
65+ break ;
66+ default :
67+ break ;
5868 }
5969 }
6070 }
61-
71+
6272 if ( ! isDotAfterVariable ) {
6373 const generalSuggestions = getGeneralSuggestions ( monaco , range ) ;
64-
6574 return {
6675 suggestions : [ ...generalSuggestions , ...variableSuggestions ]
6776 } ;
@@ -72,6 +81,4 @@ export function initCppCompletion(monaco: typeof import('monaco-editor')) {
7281 }
7382 }
7483 } ) ;
75-
76-
7784}
0 commit comments