@@ -54,6 +54,26 @@ import {
5454 undo ,
5555} from "@codemirror/commands" ;
5656import { indentUnit as indentUnitFacet } from "@codemirror/language" ;
57+ import {
58+ closeLintPanel ,
59+ nextDiagnostic ,
60+ openLintPanel ,
61+ previousDiagnostic ,
62+ } from "@codemirror/lint" ;
63+ import {
64+ LSPPlugin ,
65+ closeReferencePanel as lspCloseReferencePanel ,
66+ findReferences as lspFindReferences ,
67+ formatDocument as lspFormatDocument ,
68+ jumpToDeclaration as lspJumpToDeclaration ,
69+ jumpToDefinition as lspJumpToDefinition ,
70+ jumpToImplementation as lspJumpToImplementation ,
71+ jumpToTypeDefinition as lspJumpToTypeDefinition ,
72+ nextSignature as lspNextSignature ,
73+ prevSignature as lspPrevSignature ,
74+ renameSymbol as lspRenameSymbol ,
75+ showSignatureHelp as lspShowSignatureHelp ,
76+ } from "@codemirror/lsp-client" ;
5777import { Compartment , EditorSelection } from "@codemirror/state" ;
5878import { keymap } from "@codemirror/view" ;
5979import prompt from "dialogs/prompt" ;
@@ -132,6 +152,8 @@ const CODEMIRROR_COMMAND_MAP = new Map(
132152) ;
133153
134154registerCoreCommands ( ) ;
155+ registerLspCommands ( ) ;
156+ registerLintCommands ( ) ;
135157registerCommandsFromKeyBindings ( ) ;
136158rebuildKeymap ( ) ;
137159
@@ -834,6 +856,137 @@ function registerCoreCommands() {
834856 } ) ;
835857}
836858
859+ function registerLspCommands ( ) {
860+ addCommand ( {
861+ name : "formatDocument" ,
862+ description : "Format document (Language Server)" ,
863+ readOnly : false ,
864+ requiresView : true ,
865+ run : runLspCommand ( lspFormatDocument ) ,
866+ } ) ;
867+ addCommand ( {
868+ name : "renameSymbol" ,
869+ description : "Rename symbol (Language Server)" ,
870+ readOnly : false ,
871+ requiresView : true ,
872+ run : runLspCommand ( lspRenameSymbol ) ,
873+ } ) ;
874+ addCommand ( {
875+ name : "showSignatureHelp" ,
876+ description : "Show signature help" ,
877+ readOnly : true ,
878+ requiresView : true ,
879+ run : runLspCommand ( lspShowSignatureHelp ) ,
880+ } ) ;
881+ addCommand ( {
882+ name : "nextSignature" ,
883+ description : "Next signature" ,
884+ readOnly : true ,
885+ requiresView : true ,
886+ run : runLspCommand ( lspNextSignature , { silentOnMissing : true } ) ,
887+ } ) ;
888+ addCommand ( {
889+ name : "prevSignature" ,
890+ description : "Previous signature" ,
891+ readOnly : true ,
892+ requiresView : true ,
893+ run : runLspCommand ( lspPrevSignature , { silentOnMissing : true } ) ,
894+ } ) ;
895+ addCommand ( {
896+ name : "jumpToDefinition" ,
897+ description : "Go to definition (Language Server)" ,
898+ readOnly : true ,
899+ requiresView : true ,
900+ run : runLspCommand ( lspJumpToDefinition ) ,
901+ } ) ;
902+ addCommand ( {
903+ name : "jumpToDeclaration" ,
904+ description : "Go to declaration (Language Server)" ,
905+ readOnly : true ,
906+ requiresView : true ,
907+ run : runLspCommand ( lspJumpToDeclaration ) ,
908+ } ) ;
909+ addCommand ( {
910+ name : "jumpToTypeDefinition" ,
911+ description : "Go to type definition (Language Server)" ,
912+ readOnly : true ,
913+ requiresView : true ,
914+ run : runLspCommand ( lspJumpToTypeDefinition ) ,
915+ } ) ;
916+ addCommand ( {
917+ name : "jumpToImplementation" ,
918+ description : "Go to implementation (Language Server)" ,
919+ readOnly : true ,
920+ requiresView : true ,
921+ run : runLspCommand ( lspJumpToImplementation ) ,
922+ } ) ;
923+ addCommand ( {
924+ name : "findReferences" ,
925+ description : "Find references (Language Server)" ,
926+ readOnly : true ,
927+ requiresView : true ,
928+ run : runLspCommand ( lspFindReferences ) ,
929+ } ) ;
930+ addCommand ( {
931+ name : "closeReferencePanel" ,
932+ description : "Close references panel" ,
933+ readOnly : true ,
934+ requiresView : true ,
935+ run ( view ) {
936+ const resolvedView = resolveView ( view ) ;
937+ if ( ! resolvedView ) return false ;
938+ return lspCloseReferencePanel ( resolvedView ) ;
939+ } ,
940+ } ) ;
941+ }
942+
943+ function registerLintCommands ( ) {
944+ addCommand ( {
945+ name : "openLintPanel" ,
946+ description : "Open lint panel" ,
947+ readOnly : true ,
948+ requiresView : true ,
949+ run ( view ) {
950+ const resolvedView = resolveView ( view ) ;
951+ if ( ! resolvedView ) return false ;
952+ return openLintPanel ( resolvedView ) ;
953+ } ,
954+ } ) ;
955+ addCommand ( {
956+ name : "closeLintPanel" ,
957+ description : "Close lint panel" ,
958+ readOnly : true ,
959+ requiresView : true ,
960+ run ( view ) {
961+ const resolvedView = resolveView ( view ) ;
962+ if ( ! resolvedView ) return false ;
963+ return closeLintPanel ( resolvedView ) ;
964+ } ,
965+ } ) ;
966+ addCommand ( {
967+ name : "nextDiagnostic" ,
968+ description : "Go to next diagnostic" ,
969+ readOnly : true ,
970+ requiresView : true ,
971+ run ( view ) {
972+ const resolvedView = resolveView ( view ) ;
973+ if ( ! resolvedView ) return false ;
974+ return nextDiagnostic ( resolvedView ) ;
975+ } ,
976+ } ) ;
977+ addCommand ( {
978+ name : "previousDiagnostic" ,
979+ description : "Go to previous diagnostic" ,
980+ readOnly : true ,
981+ requiresView : true ,
982+ run ( view ) {
983+ const resolvedView = resolveView ( view ) ;
984+ if ( ! resolvedView ) return false ;
985+ return previousDiagnostic ( resolvedView ) ;
986+ } ,
987+ } ) ;
988+ }
989+
837990function registerCommandsFromKeyBindings ( ) {
838991 Object . entries ( keyBindings ) . forEach ( ( [ name , binding ] ) => {
839992 if ( commandMap . has ( name ) ) return ;
@@ -894,6 +1047,26 @@ function resolveView(view) {
8941047 return view || editorManager ?. editor || null ;
8951048}
8961049
1050+ function notifyLspUnavailable ( ) {
1051+ toast ?. ( "Language server not available" ) ;
1052+ }
1053+
1054+ function runLspCommand ( commandFn , options = { } ) {
1055+ return ( view ) => {
1056+ const resolvedView = resolveView ( view ) ;
1057+ if ( ! resolvedView ) return false ;
1058+ const plugin = LSPPlugin . get ( resolvedView ) ;
1059+ if ( ! plugin ) {
1060+ if ( ! options ?. silentOnMissing ) {
1061+ notifyLspUnavailable ( ) ;
1062+ }
1063+ return false ;
1064+ }
1065+ const result = commandFn ( resolvedView ) ;
1066+ return result !== false ;
1067+ } ;
1068+ }
1069+
8971070function humanizeCommandName ( name ) {
8981071 return name
8991072 . replace ( / ( [ a - z 0 - 9 ] ) ( [ A - Z ] ) / g, "$1 $2" )
0 commit comments