@@ -106,6 +106,12 @@ export class ExtensionManager implements vscode.Disposable {
106106 private onDidChangeActiveTextEditorSub : vscode . Disposable = new DummyDisposable ( ) ;
107107 private readonly extensionActiveCommandsEmitter = new vscode . EventEmitter < void > ( ) ;
108108 private readonly workspaceConfig : ConfigurationReader = ConfigurationReader . create ( ) ;
109+ private readonly CMAKE_LANGUAGE = "cmake" ;
110+ private readonly CMAKE_SELECTOR : vscode . DocumentSelector = [
111+ { language : this . CMAKE_LANGUAGE , scheme : "file" } ,
112+ { language : this . CMAKE_LANGUAGE , scheme : "untitled" }
113+ ] ;
114+ private languageServicesDisposables : vscode . Disposable [ ] = [ ] ;
109115
110116 private updateTouchBarVisibility ( config : TouchBarConfig ) {
111117 const touchBarVisible = config . visibility === "default" ;
@@ -119,6 +125,17 @@ export class ExtensionManager implements vscode.Disposable {
119125 * Second-phase async init
120126 */
121127 public async init ( ) {
128+ if ( this . workspaceConfig . enableLanguageServices ) {
129+ await this . enableLanguageServices ( ) ;
130+ this . workspaceConfig . onChange ( 'enableLanguageServices' , async ( value ) => {
131+ if ( value ) {
132+ await this . enableLanguageServices ( ) ;
133+ } else {
134+ this . disposeLanguageServices ( ) ;
135+ }
136+ } ) ;
137+ }
138+
122139 this . updateTouchBarVisibility ( this . workspaceConfig . touchbar ) ;
123140 this . workspaceConfig . onChange ( 'touchbar' , config => this . updateTouchBarVisibility ( config ) ) ;
124141
@@ -357,6 +374,7 @@ export class ExtensionManager implements vscode.Disposable {
357374 private activeTestPresetSub : vscode . Disposable = new DummyDisposable ( ) ;
358375 private activePackagePresetSub : vscode . Disposable = new DummyDisposable ( ) ;
359376 private activeWorkflowPresetSub : vscode . Disposable = new DummyDisposable ( ) ;
377+ private enableLanguageServicesSub : vscode . Disposable = new DummyDisposable ( ) ;
360378
361379 // Watch the code model so that we may update the tree view
362380 // <fspath, sub>
@@ -379,6 +397,83 @@ export class ExtensionManager implements vscode.Disposable {
379397 private cppToolsAPI ?: cpt . CppToolsApi ;
380398 private configProviderRegistered ?: boolean = false ;
381399
400+ private async enableLanguageServices ( ) {
401+ try {
402+ const languageServices = await LanguageServiceData . create ( ) ;
403+ this . languageServicesDisposables . push ( vscode . languages . registerHoverProvider (
404+ this . CMAKE_SELECTOR ,
405+ languageServices
406+ ) ) ;
407+ this . languageServicesDisposables . push ( vscode . languages . registerCompletionItemProvider (
408+ this . CMAKE_SELECTOR ,
409+ languageServices
410+ ) ) ;
411+ } catch {
412+ log . error (
413+ localize (
414+ "language.service.failed" ,
415+ "Failed to initialize language services"
416+ )
417+ ) ;
418+ }
419+
420+ this . languageServicesDisposables . push ( vscode . languages . setLanguageConfiguration (
421+ this . CMAKE_LANGUAGE ,
422+ {
423+ indentationRules : {
424+ // ^(.*\*/)?\s*\ }.*$
425+ decreaseIndentPattern : / ^ ( .* \* \/ ) ? \s * \} .* $ / ,
426+ // ^.*\{[^}"']*$
427+ increaseIndentPattern : / ^ .* \{ [ ^ } " ' ] * $ /
428+ } ,
429+ wordPattern :
430+ / ( - ? \d * \. \d \w * ) | ( [ ^ \` \~ \! \@ \# \% \^ \& \* \( \) \- \= \+ \[ \{ \] \} \\ \| \; \: \' \" \, \. \< \> \/ \? \s ] + ) / g,
431+ comments : {
432+ lineComment : "#"
433+ } ,
434+ brackets : [
435+ [ "{" , "}" ] ,
436+ [ "(" , ")" ]
437+ ] ,
438+
439+ __electricCharacterSupport : {
440+ brackets : [
441+ {
442+ tokenType : "delimiter.curly.ts" ,
443+ open : "{" ,
444+ close : "}" ,
445+ isElectric : true
446+ } ,
447+ {
448+ tokenType : "delimiter.square.ts" ,
449+ open : "[" ,
450+ close : "]" ,
451+ isElectric : true
452+ } ,
453+ {
454+ tokenType : "delimiter.paren.ts" ,
455+ open : "(" ,
456+ close : ")" ,
457+ isElectric : true
458+ }
459+ ]
460+ } ,
461+
462+ __characterPairSupport : {
463+ autoClosingPairs : [
464+ { open : "{" , close : "}" } ,
465+ { open : "(" , close : ")" } ,
466+ { open : '"' , close : '"' , notIn : [ "string" ] }
467+ ]
468+ }
469+ }
470+ ) ) ;
471+ }
472+
473+ private disposeLanguageServices ( ) {
474+ this . languageServicesDisposables . forEach ( sub => sub . dispose ( ) ) ;
475+ }
476+
382477 private getProjectsForWorkspaceFolder ( folder ?: vscode . WorkspaceFolder ) : CMakeProject [ ] | undefined {
383478 folder = this . getWorkspaceFolder ( folder ) ;
384479 return this . projectController . getProjectsForWorkspaceFolder ( folder ) ;
@@ -554,6 +649,7 @@ export class ExtensionManager implements vscode.Disposable {
554649 */
555650 async asyncDispose ( ) {
556651 this . disposeSubs ( ) ;
652+ this . disposeLanguageServices ( ) ;
557653 this . codeModelUpdateSubs . forEach (
558654 subs => subs . forEach (
559655 sub => sub . dispose ( )
@@ -736,7 +832,7 @@ export class ExtensionManager implements vscode.Disposable {
736832
737833 private disposeSubs ( ) {
738834 util . disposeAll ( this . projectSubscriptions ) ;
739- for ( const sub of [ this . statusMessageSub , this . targetNameSub , this . buildTypeSub , this . launchTargetSub , this . ctestEnabledSub , this . isBusySub , this . activeConfigurePresetSub , this . activeBuildPresetSub , this . activeTestPresetSub , this . activePackagePresetSub , this . activeWorkflowPresetSub ] ) {
835+ for ( const sub of [ this . statusMessageSub , this . targetNameSub , this . buildTypeSub , this . launchTargetSub , this . ctestEnabledSub , this . isBusySub , this . activeConfigurePresetSub , this . activeBuildPresetSub , this . activeTestPresetSub , this . activePackagePresetSub , this . activeWorkflowPresetSub , this . enableLanguageServicesSub ] ) {
740836 sub . dispose ( ) ;
741837 }
742838 }
@@ -834,6 +930,7 @@ export class ExtensionManager implements vscode.Disposable {
834930 this . activeTestPresetSub = new DummyDisposable ( ) ;
835931 this . activePackagePresetSub = new DummyDisposable ( ) ;
836932 this . activeWorkflowPresetSub = new DummyDisposable ( ) ;
933+ this . enableLanguageServicesSub = new DummyDisposable ( ) ;
837934 this . statusBar . setActiveKitName ( '' ) ;
838935 this . statusBar . setConfigurePresetName ( '' ) ;
839936 this . statusBar . setBuildPresetName ( '' ) ;
@@ -2400,53 +2497,6 @@ export async function activate(context: vscode.ExtensionContext): Promise<api.CM
24002497 } ) ;
24012498 }
24022499
2403- const CMAKE_LANGUAGE = "cmake" ;
2404- const CMAKE_SELECTOR : vscode . DocumentSelector = [
2405- { language : CMAKE_LANGUAGE , scheme : 'file' } ,
2406- { language : CMAKE_LANGUAGE , scheme : 'untitled' }
2407- ] ;
2408-
2409- try {
2410- const languageServices = await LanguageServiceData . create ( ) ;
2411- vscode . languages . registerHoverProvider ( CMAKE_SELECTOR , languageServices ) ;
2412- vscode . languages . registerCompletionItemProvider ( CMAKE_SELECTOR , languageServices ) ;
2413- } catch {
2414- log . error ( localize ( 'language.service.failed' , 'Failed to initialize language services' ) ) ;
2415- }
2416-
2417- vscode . languages . setLanguageConfiguration ( CMAKE_LANGUAGE , {
2418- indentationRules : {
2419- // ^(.*\*/)?\s*\ }.*$
2420- decreaseIndentPattern : / ^ ( .* \* \/ ) ? \s * \} .* $ / ,
2421- // ^.*\{[^}"']*$
2422- increaseIndentPattern : / ^ .* \{ [ ^ } " ' ] * $ /
2423- } ,
2424- wordPattern : / ( - ? \d * \. \d \w * ) | ( [ ^ \` \~ \! \@ \# \% \^ \& \* \( \) \- \= \+ \[ \{ \] \} \\ \| \; \: \' \" \, \. \< \> \/ \? \s ] + ) / g,
2425- comments : {
2426- lineComment : '#'
2427- } ,
2428- brackets : [
2429- [ '{' , '}' ] ,
2430- [ '(' , ')' ]
2431- ] ,
2432-
2433- __electricCharacterSupport : {
2434- brackets : [
2435- { tokenType : 'delimiter.curly.ts' , open : '{' , close : '}' , isElectric : true } ,
2436- { tokenType : 'delimiter.square.ts' , open : '[' , close : ']' , isElectric : true } ,
2437- { tokenType : 'delimiter.paren.ts' , open : '(' , close : ')' , isElectric : true }
2438- ]
2439- } ,
2440-
2441- __characterPairSupport : {
2442- autoClosingPairs : [
2443- { open : '{' , close : '}' } ,
2444- { open : '(' , close : ')' } ,
2445- { open : '"' , close : '"' , notIn : [ 'string' ] }
2446- ]
2447- }
2448- } ) ;
2449-
24502500 if ( vscode . workspace . getConfiguration ( 'cmake' ) . get ( 'showOptionsMovedNotification' ) ) {
24512501 void vscode . window . showInformationMessage (
24522502 localize ( 'options.moved.notification.body' , "Some status bar options in CMake Tools have now moved to the Project Status View in the CMake Tools sidebar. You can customize your view with the 'cmake.options' property in settings." ) ,
0 commit comments