@@ -2,7 +2,7 @@ import * as vscode from "vscode";
22import * as lc from "vscode-languageclient/node" ;
33
44import * as commands from "./commands" ;
5- import { Ctx , Workspace } from "./ctx" ;
5+ import { CommandFactory , Ctx , Workspace } from "./ctx" ;
66import { isRustDocument } from "./util" ;
77import { activateTaskProvider } from "./tasks" ;
88import { setContextValue } from "./util" ;
@@ -57,7 +57,7 @@ export async function activate(
5757 }
5858 : { kind : "Workspace Folder" } ;
5959
60- const ctx = new Ctx ( context , workspace ) ;
60+ const ctx = new Ctx ( context , workspace , createCommands ( ) ) ;
6161 // VS Code doesn't show a notification when an extension fails to activate
6262 // so we do it ourselves.
6363 const api = await activateServer ( ctx ) . catch ( ( err ) => {
@@ -75,8 +75,6 @@ async function activateServer(ctx: Ctx): Promise<RustAnalyzerExtensionApi> {
7575 ctx . pushExtCleanup ( activateTaskProvider ( ctx . config ) ) ;
7676 }
7777
78- await initCommonContext ( ctx ) ;
79-
8078 vscode . workspace . onDidChangeConfiguration (
8179 async ( _ ) => {
8280 await ctx
@@ -91,85 +89,78 @@ async function activateServer(ctx: Ctx): Promise<RustAnalyzerExtensionApi> {
9189 return ctx . clientFetcher ( ) ;
9290}
9391
94- async function initCommonContext ( ctx : Ctx ) {
95- // Register a "dumb" onEnter command for the case where server fails to
96- // start.
97- //
98- // FIXME: refactor command registration code such that commands are
99- // **always** registered, even if the server does not start. Use API like
100- // this perhaps?
101- //
102- // ```TypeScript
103- // registerCommand(
104- // factory: (Ctx) => ((Ctx) => any),
105- // fallback: () => any = () => vscode.window.showErrorMessage(
106- // "rust-analyzer is not available"
107- // ),
108- // )
109- const defaultOnEnter = vscode . commands . registerCommand ( "rust-analyzer.onEnter" , ( ) =>
110- vscode . commands . executeCommand ( "default:type" , { text : "\n" } )
111- ) ;
112- ctx . pushExtCleanup ( defaultOnEnter ) ;
113-
114- // Commands which invokes manually via command palette, shortcut, etc.
115- ctx . registerCommand ( "reload" , ( _ ) => async ( ) => {
116- void vscode . window . showInformationMessage ( "Reloading rust-analyzer..." ) ;
117- // FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
118- await ctx . disposeClient ( ) ;
119- await ctx . activate ( ) ;
120- } ) ;
121-
122- ctx . registerCommand ( "startServer" , ( _ ) => async ( ) => {
123- await ctx . activate ( ) ;
124- } ) ;
125- ctx . registerCommand ( "stopServer" , ( _ ) => async ( ) => {
126- // FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
127- await ctx . disposeClient ( ) ;
128- ctx . setServerStatus ( {
129- health : "ok" ,
130- quiescent : true ,
131- message : "server is not running" ,
132- } ) ;
133- } ) ;
134- ctx . registerCommand ( "analyzerStatus" , commands . analyzerStatus ) ;
135- ctx . registerCommand ( "memoryUsage" , commands . memoryUsage ) ;
136- ctx . registerCommand ( "shuffleCrateGraph" , commands . shuffleCrateGraph ) ;
137- ctx . registerCommand ( "reloadWorkspace" , commands . reloadWorkspace ) ;
138- ctx . registerCommand ( "matchingBrace" , commands . matchingBrace ) ;
139- ctx . registerCommand ( "joinLines" , commands . joinLines ) ;
140- ctx . registerCommand ( "parentModule" , commands . parentModule ) ;
141- ctx . registerCommand ( "syntaxTree" , commands . syntaxTree ) ;
142- ctx . registerCommand ( "viewHir" , commands . viewHir ) ;
143- ctx . registerCommand ( "viewFileText" , commands . viewFileText ) ;
144- ctx . registerCommand ( "viewItemTree" , commands . viewItemTree ) ;
145- ctx . registerCommand ( "viewCrateGraph" , commands . viewCrateGraph ) ;
146- ctx . registerCommand ( "viewFullCrateGraph" , commands . viewFullCrateGraph ) ;
147- ctx . registerCommand ( "expandMacro" , commands . expandMacro ) ;
148- ctx . registerCommand ( "run" , commands . run ) ;
149- ctx . registerCommand ( "copyRunCommandLine" , commands . copyRunCommandLine ) ;
150- ctx . registerCommand ( "debug" , commands . debug ) ;
151- ctx . registerCommand ( "newDebugConfig" , commands . newDebugConfig ) ;
152- ctx . registerCommand ( "openDocs" , commands . openDocs ) ;
153- ctx . registerCommand ( "openCargoToml" , commands . openCargoToml ) ;
154- ctx . registerCommand ( "peekTests" , commands . peekTests ) ;
155- ctx . registerCommand ( "moveItemUp" , commands . moveItemUp ) ;
156- ctx . registerCommand ( "moveItemDown" , commands . moveItemDown ) ;
157- ctx . registerCommand ( "cancelFlycheck" , commands . cancelFlycheck ) ;
158-
159- ctx . registerCommand ( "ssr" , commands . ssr ) ;
160- ctx . registerCommand ( "serverVersion" , commands . serverVersion ) ;
161-
162- // Internal commands which are invoked by the server.
163- ctx . registerCommand ( "runSingle" , commands . runSingle ) ;
164- ctx . registerCommand ( "debugSingle" , commands . debugSingle ) ;
165- ctx . registerCommand ( "showReferences" , commands . showReferences ) ;
166- ctx . registerCommand ( "applySnippetWorkspaceEdit" , commands . applySnippetWorkspaceEditCommand ) ;
167- ctx . registerCommand ( "resolveCodeAction" , commands . resolveCodeAction ) ;
168- ctx . registerCommand ( "applyActionGroup" , commands . applyActionGroup ) ;
169- ctx . registerCommand ( "gotoLocation" , commands . gotoLocation ) ;
170-
171- ctx . registerCommand ( "linkToCommand" , commands . linkToCommand ) ;
92+ function createCommands ( ) : Record < string , CommandFactory > {
93+ return {
94+ onEnter : {
95+ enabled : commands . onEnter ,
96+ disabled : ( _ ) => ( ) => vscode . commands . executeCommand ( "default:type" , { text : "\n" } ) ,
97+ } ,
98+ reload : {
99+ enabled : ( ctx ) => async ( ) => {
100+ void vscode . window . showInformationMessage ( "Reloading rust-analyzer..." ) ;
101+ // FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
102+ await ctx . stop ( ) ;
103+ await ctx . activate ( ) ;
104+ } ,
105+ disabled : ( ctx ) => async ( ) => {
106+ void vscode . window . showInformationMessage ( "Reloading rust-analyzer..." ) ;
107+ await ctx . activate ( ) ;
108+ } ,
109+ } ,
110+ startServer : {
111+ enabled : ( ctx ) => async ( ) => {
112+ await ctx . activate ( ) ;
113+ } ,
114+ disabled : ( ctx ) => async ( ) => {
115+ await ctx . activate ( ) ;
116+ } ,
117+ } ,
118+ stopServer : {
119+ enabled : ( ctx ) => async ( ) => {
120+ // FIXME: We should re-use the client, that is ctx.deactivate() if none of the configs have changed
121+ await ctx . stop ( ) ;
122+ ctx . setServerStatus ( {
123+ health : "ok" ,
124+ quiescent : true ,
125+ message : "server is not running" ,
126+ } ) ;
127+ } ,
128+ } ,
172129
173- defaultOnEnter . dispose ( ) ;
174- ctx . registerCommand ( "onEnter" , commands . onEnter ) ;
130+ analyzerStatus : { enabled : commands . analyzerStatus } ,
131+ memoryUsage : { enabled : commands . memoryUsage } ,
132+ shuffleCrateGraph : { enabled : commands . shuffleCrateGraph } ,
133+ reloadWorkspace : { enabled : commands . reloadWorkspace } ,
134+ matchingBrace : { enabled : commands . matchingBrace } ,
135+ joinLines : { enabled : commands . joinLines } ,
136+ parentModule : { enabled : commands . parentModule } ,
137+ syntaxTree : { enabled : commands . syntaxTree } ,
138+ viewHir : { enabled : commands . viewHir } ,
139+ viewFileText : { enabled : commands . viewFileText } ,
140+ viewItemTree : { enabled : commands . viewItemTree } ,
141+ viewCrateGraph : { enabled : commands . viewCrateGraph } ,
142+ viewFullCrateGraph : { enabled : commands . viewFullCrateGraph } ,
143+ expandMacro : { enabled : commands . expandMacro } ,
144+ run : { enabled : commands . run } ,
145+ copyRunCommandLine : { enabled : commands . copyRunCommandLine } ,
146+ debug : { enabled : commands . debug } ,
147+ newDebugConfig : { enabled : commands . newDebugConfig } ,
148+ openDocs : { enabled : commands . openDocs } ,
149+ openCargoToml : { enabled : commands . openCargoToml } ,
150+ peekTests : { enabled : commands . peekTests } ,
151+ moveItemUp : { enabled : commands . moveItemUp } ,
152+ moveItemDown : { enabled : commands . moveItemDown } ,
153+ cancelFlycheck : { enabled : commands . cancelFlycheck } ,
154+ ssr : { enabled : commands . ssr } ,
155+ serverVersion : { enabled : commands . serverVersion } ,
156+ // Internal commands which are invoked by the server.
157+ applyActionGroup : { enabled : commands . applyActionGroup } ,
158+ applySnippetWorkspaceEdit : { enabled : commands . applySnippetWorkspaceEditCommand } ,
159+ debugSingle : { enabled : commands . debugSingle } ,
160+ gotoLocation : { enabled : commands . gotoLocation } ,
161+ linkToCommand : { enabled : commands . linkToCommand } ,
162+ resolveCodeAction : { enabled : commands . resolveCodeAction } ,
163+ runSingle : { enabled : commands . runSingle } ,
164+ showReferences : { enabled : commands . showReferences } ,
165+ } ;
175166}
0 commit comments