@ -45,6 +45,28 @@ use std::{
sync ::Arc ,
sync ::Arc ,
} ;
} ;
/// Gets the first language server that is attached to a document which supports a specific feature.
/// If there is no configured language server that supports the feature, this displays a status message.
/// Using this macro in a context where the editor automatically queries the LSP
/// (instead of when the user explicitly does so via a keybind like `gd`)
/// will spam the "No configured language server supports <feature>" status message confusingly.
#[ macro_export ]
macro_rules! language_server_with_feature {
( $editor :expr , $doc :expr , $feature :expr ) = > { {
let language_server = $doc . language_servers_with_feature ( $feature ) . next ( ) ;
match language_server {
Some ( language_server ) = > language_server ,
None = > {
$editor . set_status ( format! (
"No configured language server supports {}" ,
$feature
) ) ;
return ;
}
}
} } ;
}
impl ui ::menu ::Item for lsp ::Location {
impl ui ::menu ::Item for lsp ::Location {
/// Current working directory.
/// Current working directory.
type Data = PathBuf ;
type Data = PathBuf ;
@ -361,36 +383,38 @@ pub fn symbol_picker(cx: &mut Context) {
let mut futures : FuturesUnordered < _ > = doc
let mut futures : FuturesUnordered < _ > = doc
. language_servers_with_feature ( LanguageServerFeature ::DocumentSymbols )
. language_servers_with_feature ( LanguageServerFeature ::DocumentSymbols )
. filter ( | ls | seen_language_servers . insert ( ls . id ( ) ) )
. filter ( | ls | seen_language_servers . insert ( ls . id ( ) ) )
. filter_map ( | ls | {
. map ( | language_server | {
let request = ls . document_symbols ( doc . identifier ( ) ) ? ;
let request = language_server . document_symbols ( doc . identifier ( ) ) . unwrap ( ) ;
Some ( ( request , ls . offset_encoding ( ) , doc . identifier ( ) ) )
let offset_encoding = language_server . offset_encoding ( ) ;
} )
let doc_id = doc . identifier ( ) ;
. map ( | ( request , offset_encoding , doc_id ) | async move {
let json = request . await ? ;
async move {
let response : Option < lsp ::DocumentSymbolResponse > = serde_json ::from_value ( json ) ? ;
let json = request . await ? ;
let symbols = match response {
let response : Option < lsp ::DocumentSymbolResponse > = serde_json ::from_value ( json ) ? ;
Some ( symbols ) = > symbols ,
let symbols = match response {
None = > return anyhow ::Ok ( vec! [ ] ) ,
Some ( symbols ) = > symbols ,
} ;
None = > return anyhow ::Ok ( vec! [ ] ) ,
// lsp has two ways to represent symbols (flat/nested)
} ;
// convert the nested variant to flat, so that we have a homogeneous list
// lsp has two ways to represent symbols (flat/nested)
let symbols = match symbols {
// convert the nested variant to flat, so that we have a homogeneous list
lsp ::DocumentSymbolResponse ::Flat ( symbols ) = > symbols
let symbols = match symbols {
. into_iter ( )
lsp ::DocumentSymbolResponse ::Flat ( symbols ) = > symbols
. map ( | symbol | SymbolInformationItem {
. into_iter ( )
symbol ,
. map ( | symbol | SymbolInformationItem {
offset_encoding ,
symbol ,
} )
offset_encoding ,
. collect ( ) ,
} )
lsp ::DocumentSymbolResponse ::Nested ( symbols ) = > {
. collect ( ) ,
let mut flat_symbols = Vec ::new ( ) ;
lsp ::DocumentSymbolResponse ::Nested ( symbols ) = > {
for symbol in symbols {
let mut flat_symbols = Vec ::new ( ) ;
nested_to_flat ( & mut flat_symbols , & doc_id , symbol , offset_encoding )
for symbol in symbols {
nested_to_flat ( & mut flat_symbols , & doc_id , symbol , offset_encoding )
}
flat_symbols
}
}
flat_symbols
} ;
}
Ok ( symbols )
} ;
}
Ok ( symbols )
} )
} )
. collect ( ) ;
. collect ( ) ;
let current_url = doc . url ( ) ;
let current_url = doc . url ( ) ;
@ -425,20 +449,24 @@ pub fn workspace_symbol_picker(cx: &mut Context) {
let mut futures : FuturesUnordered < _ > = doc
let mut futures : FuturesUnordered < _ > = doc
. language_servers_with_feature ( LanguageServerFeature ::WorkspaceSymbols )
. language_servers_with_feature ( LanguageServerFeature ::WorkspaceSymbols )
. filter ( | ls | seen_language_servers . insert ( ls . id ( ) ) )
. filter ( | ls | seen_language_servers . insert ( ls . id ( ) ) )
. filter_map ( | ls | Some ( ( ls . workspace_symbols ( pattern . clone ( ) ) ? , ls . offset_encoding ( ) ) ) )
. map ( | language_server | {
. map ( | ( request , offset_encoding ) | async move {
let request = language_server . workspace_symbols ( pattern . clone ( ) ) . unwrap ( ) ;
let json = request . await ? ;
let offset_encoding = language_server . offset_encoding ( ) ;
async move {
let response = serde_json ::from_value ::< Option < Vec < lsp ::SymbolInformation > > > ( json ) ?
let json = request . await ? ;
. unwrap_or_default ( )
. into_iter ( )
let response =
. map ( | symbol | SymbolInformationItem {
serde_json ::from_value ::< Option < Vec < lsp ::SymbolInformation > > > ( json ) ?
symbol ,
. unwrap_or_default ( )
offset_encoding ,
. into_iter ( )
} )
. map ( | symbol | SymbolInformationItem {
. collect ( ) ;
symbol ,
offset_encoding ,
anyhow ::Ok ( response )
} )
. collect ( ) ;
anyhow ::Ok ( response )
}
} )
} )
. collect ( ) ;
. collect ( ) ;
@ -1043,22 +1071,19 @@ where
F : Future < Output = helix_lsp ::Result < serde_json ::Value > > + ' static + Send ,
F : Future < Output = helix_lsp ::Result < serde_json ::Value > > + ' static + Send ,
{
{
let ( view , doc ) = current ! ( cx . editor ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
if let Some ( ( future , offset_encoding ) ) =
doc . run_on_first_supported_language_server ( view . id , feature , | ls , encoding , pos , doc_id | {
let language_server = language_server_with_feature ! ( cx . editor , doc , feature ) ;
Some ( ( request_provider ( ls , pos , doc_id ) ? , encoding ) )
let offset_encoding = language_server . offset_encoding ( ) ;
} )
let pos = doc . position ( view . id , offset_encoding ) ;
{
let future = request_provider ( language_server , pos , doc . identifier ( ) ) . unwrap ( ) ;
cx . callback (
future ,
cx . callback (
move | editor , compositor , response : Option < lsp ::GotoDefinitionResponse > | {
future ,
let items = to_locations ( response ) ;
move | editor , compositor , response : Option < lsp ::GotoDefinitionResponse > | {
goto_impl ( editor , compositor , items , offset_encoding ) ;
let items = to_locations ( response ) ;
} ,
goto_impl ( editor , compositor , items , offset_encoding ) ;
) ;
} ,
} else {
) ;
cx . editor
. set_error ( "No configured language server supports {feature}" ) ;
}
}
}
pub fn goto_declaration ( cx : & mut Context ) {
pub fn goto_declaration ( cx : & mut Context ) {
@ -1096,32 +1121,29 @@ pub fn goto_implementation(cx: &mut Context) {
pub fn goto_reference ( cx : & mut Context ) {
pub fn goto_reference ( cx : & mut Context ) {
let config = cx . editor . config ( ) ;
let config = cx . editor . config ( ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
if let Some ( ( future , offset_encoding ) ) = doc . run_on_first_supported_language_server (
view . id ,
// TODO could probably support multiple language servers,
LanguageServerFeature ::GotoReference ,
// not sure if there's a real practical use case for this though
| ls , encoding , pos , doc_id | {
let language_server =
Some ( (
language_server_with_feature ! ( cx . editor , doc , LanguageServerFeature ::GotoReference ) ;
ls . goto_reference (
let offset_encoding = language_server . offset_encoding ( ) ;
doc_id ,
let pos = doc . position ( view . id , offset_encoding ) ;
pos ,
let future = language_server
config . lsp . goto_reference_include_declaration ,
. goto_reference (
None ,
doc . identifier ( ) ,
) ? ,
pos ,
encoding ,
config . lsp . goto_reference_include_declaration ,
) )
None ,
)
. unwrap ( ) ;
cx . callback (
future ,
move | editor , compositor , response : Option < Vec < lsp ::Location > > | {
let items = response . unwrap_or_default ( ) ;
goto_impl ( editor , compositor , items , offset_encoding ) ;
} ,
} ,
) {
) ;
cx . callback (
future ,
move | editor , compositor , response : Option < Vec < lsp ::Location > > | {
let items = response . unwrap_or_default ( ) ;
goto_impl ( editor , compositor , items , offset_encoding ) ;
} ,
) ;
} else {
cx . editor
. set_error ( "No configured language server supports goto-reference" ) ;
}
}
}
#[ derive(PartialEq, Eq, Clone, Copy) ]
#[ derive(PartialEq, Eq, Clone, Copy) ]
@ -1145,19 +1167,15 @@ pub fn signature_help_impl(cx: &mut Context, invoked: SignatureHelpInvoked) {
language_server . text_document_signature_help ( doc . identifier ( ) , pos , None )
language_server . text_document_signature_help ( doc . identifier ( ) , pos , None )
} ) ;
} ) ;
let future = match future {
let Some ( future ) = future else {
Some ( future ) = > future . boxed ( ) ,
// Do not show the message if signature help was invoked
None = > {
// automatically on backspace, trigger characters, etc.
// Do not show the message if signature help was invoked
if invoked = = SignatureHelpInvoked ::Manual {
// automatically on backspace, trigger characters, etc.
cx . editor . set_error ( "No configured language server supports signature-help" ) ;
if invoked = = SignatureHelpInvoked ::Manual {
cx . editor
. set_error ( "No configured language server supports signature-help" ) ;
}
return ;
}
}
return ;
} ;
} ;
signature_help_impl_with_future ( cx , future , invoked ) ;
signature_help_impl_with_future ( cx , future . boxed ( ) , invoked ) ;
}
}
pub fn signature_help_impl_with_future (
pub fn signature_help_impl_with_future (
@ -1272,22 +1290,14 @@ pub fn signature_help_impl_with_future(
pub fn hover ( cx : & mut Context ) {
pub fn hover ( cx : & mut Context ) {
let ( view , doc ) = current ! ( cx . editor ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
// TODO support multiple language servers (merge UI somehow)
let language_server =
language_server_with_feature ! ( cx . editor , doc , LanguageServerFeature ::Hover ) ;
// TODO: factor out a doc.position_identifier() that returns lsp::TextDocumentPositionIdentifier
// TODO: factor out a doc.position_identifier() that returns lsp::TextDocumentPositionIdentifier
let request = doc
let pos = doc . position ( view . id , language_server . offset_encoding ( ) ) ;
. language_servers_with_feature ( LanguageServerFeature ::Hover )
let future = language_server
. find_map ( | language_server | {
. text_document_hover ( doc . identifier ( ) , pos , None )
let pos = doc . position ( view . id , language_server . offset_encoding ( ) ) ;
. unwrap ( ) ;
language_server . text_document_hover ( doc . identifier ( ) , pos , None )
} ) ;
let future = match request {
Some ( future ) = > future ,
None = > {
cx . editor
. set_error ( "No configured language server supports hover" ) ;
return ;
}
} ;
cx . callback (
cx . callback (
future ,
future ,
@ -1381,34 +1391,26 @@ pub fn rename_symbol(cx: &mut Context) {
return ;
return ;
}
}
let ( view , doc ) = current ! ( cx . editor ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
let request = doc
let Some ( language_server ) = doc
. language_servers_with_feature ( LanguageServerFeature ::RenameSymbol )
. language_servers_with_feature ( LanguageServerFeature ::RenameSymbol )
. find_map ( | language_server | {
. find ( | ls | language_server_id . is_none ( ) | | Some ( ls . id ( ) ) = = language_server_id )
if let Some ( language_server_id ) = language_server_id {
else {
if language_server . id ( ) ! = language_server_id {
cx . editor . set_error ( "No configured language server supports symbol renaming" ) ;
return None ;
return ;
}
} ;
}
let offset_encoding = language_server . offset_encoding ( ) ;
let offset_encoding = language_server . offset_encoding ( ) ;
let pos = doc . position ( view . id , offset_encoding ) ;
let pos = doc . position ( view . id , offset_encoding ) ;
let future = language_server . rename_symbol (
let future = language_server
doc . identifier ( ) ,
. rename_symbol ( doc . identifier ( ) , pos , input . to_string ( ) )
pos ,
. unwrap ( ) ;
input . to_string ( ) ,
) ? ;
match block_on ( future ) {
Some ( ( future , offset_encoding ) )
Ok ( edits ) = > {
} ) ;
let _ = apply_workspace_edit ( cx . editor , offset_encoding , & edits ) ;
if let Some ( ( future , offset_encoding ) ) = request {
match block_on ( future ) {
Ok ( edits ) = > {
let _ = apply_workspace_edit ( cx . editor , offset_encoding , & edits ) ;
}
Err ( err ) = > cx . editor . set_error ( err . to_string ( ) ) ,
}
}
} else {
Err ( err ) = > cx . editor . set_error ( err . to_string ( ) ) ,
cx . editor
. set_error ( "No configured language server supports symbol renaming" ) ;
}
}
} ,
} ,
)
)
@ -1417,20 +1419,28 @@ pub fn rename_symbol(cx: &mut Context) {
Box ::new ( prompt )
Box ::new ( prompt )
}
}
let ( view , doc ) = current ! ( cx . editor ) ;
let ( view , doc ) = current _ref ! ( cx . editor ) ;
let prepare_rename_reque st = doc
let language_server_with_ prepare_rename_suppor t = doc
. language_servers_with_feature ( LanguageServerFeature ::RenameSymbol )
. language_servers_with_feature ( LanguageServerFeature ::RenameSymbol )
. find_map ( | language_server | {
. find ( | ls | {
let offset_encoding = language_server . offset_encoding ( ) ;
matches! (
let pos = doc . position ( view . id , offset_encoding ) ;
ls . capabilities ( ) . rename_provider ,
let future = language_server . prepare_rename ( doc . identifier ( ) , pos ) ? ;
Some ( lsp ::OneOf ::Right ( lsp ::RenameOptions {
Some ( ( future , offset_encoding , language_server . id ( ) ) )
prepare_provider : Some ( true ) ,
..
} ) )
)
} ) ;
} ) ;
match prepare_rename_request {
if let Some ( language_server ) = language_server_with_prepare_rename_support {
// Language server supports textDocument/prepareRename, use it.
let ls_id = language_server . id ( ) ;
Some ( ( future , offset_encoding , ls_id ) ) = > cx . callback (
let offset_encoding = language_server . offset_encoding ( ) ;
let pos = doc . position ( view . id , offset_encoding ) ;
let future = language_server
. prepare_rename ( doc . identifier ( ) , pos )
. unwrap ( ) ;
cx . callback (
future ,
future ,
move | editor , compositor , response : Option < lsp ::PrepareRenameResponse > | {
move | editor , compositor , response : Option < lsp ::PrepareRenameResponse > | {
let prefill = match get_prefill_from_lsp_response ( editor , offset_encoding , response )
let prefill = match get_prefill_from_lsp_response ( editor , offset_encoding , response )
@ -1446,38 +1456,23 @@ pub fn rename_symbol(cx: &mut Context) {
compositor . push ( prompt ) ;
compositor . push ( prompt ) ;
} ,
} ,
) ,
) ;
// Language server does not support textDocument/prepareRename, fall back
} else {
// to word boundary selection.
let prefill = get_prefill_from_word_boundary ( cx . editor ) ;
None = > {
let prompt = create_rename_prompt ( cx . editor , prefill , None ) ;
let prefill = get_prefill_from_word_boundary ( cx . editor ) ;
cx . push_layer ( prompt ) ;
}
let prompt = create_rename_prompt ( cx . editor , prefill , None ) ;
cx . push_layer ( prompt ) ;
}
} ;
}
}
pub fn select_references_to_symbol_under_cursor ( cx : & mut Context ) {
pub fn select_references_to_symbol_under_cursor ( cx : & mut Context ) {
let ( view , doc ) = current ! ( cx . editor ) ;
let ( view , doc ) = current ! ( cx . editor ) ;
let future_offset_encoding = doc
let language_server =
. language_servers_with_feature ( LanguageServerFeature ::DocumentHighlight )
language_server_with_feature ! ( cx . editor , doc , LanguageServerFeature ::DocumentHighlight ) ;
. find_map ( | language_server | {
let offset_encoding = language_server . offset_encoding ( ) ;
let offset_encoding = language_server . offset_encoding ( ) ;
let pos = doc . position ( view . id , offset_encoding ) ;
let pos = doc . position ( view . id , offset_encoding ) ;
let future = language_server
let future =
. text_document_document_highlight ( doc . identifier ( ) , pos , None )
language_server . text_document_document_highlight ( doc . identifier ( ) , pos , None ) ? ;
. unwrap ( ) ;
Some ( ( future , offset_encoding ) )
} ) ;
let ( future , offset_encoding ) = match future_offset_encoding {
Some ( future_offset_encoding ) = > future_offset_encoding ,
None = > {
cx . editor
. set_error ( "No configured language server supports document-highlight" ) ;
return ;
}
} ;
cx . callback (
cx . callback (
future ,
future ,
@ -1532,16 +1527,9 @@ fn compute_inlay_hints_for_view(
let view_id = view . id ;
let view_id = view . id ;
let doc_id = view . doc ;
let doc_id = view . doc ;
let mut language_servers = doc . language_servers_with_feature ( LanguageServerFeature ::InlayHints ) ;
let language_server = doc
let language_server = language_servers . find ( | language_server | {
. language_servers_with_feature ( LanguageServerFeature ::InlayHints )
matches! (
. next ( ) ? ;
language_server . capabilities ( ) . inlay_hint_provider ,
Some (
lsp ::OneOf ::Left ( true )
| lsp ::OneOf ::Right ( lsp ::InlayHintServerCapabilities ::Options ( _ ) )
)
)
} ) ? ;
let doc_text = doc . text ( ) ;
let doc_text = doc . text ( ) ;
let len_lines = doc_text . len_lines ( ) ;
let len_lines = doc_text . len_lines ( ) ;