Skip to content

Instantly share code, notes, and snippets.

@runiq
Last active June 23, 2020 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save runiq/12daf2701b6483f56aac90b50553aa5a to your computer and use it in GitHub Desktop.
Save runiq/12daf2701b6483f56aac90b50553aa5a to your computer and use it in GitHub Desktop.
Patch for LCN's text_document_document_symbol()
diff --git i/src/language_server_protocol.rs w/src/language_server_protocol.rs
index 9922afa..e2916d9 100644
--- i/src/language_server_protocol.rs
+++ w/src/language_server_protocol.rs
@@ -1420,111 +1420,46 @@ impl LanguageClient {
let syms = <lsp_types::request::DocumentSymbolRequest as lsp_types::request::Request>::Result::deserialize(&result)?;
let title = format!("[LC]: symbols for {}", filename);
- let selection_ui = self.get(|state| state.selection_ui)?;
- let selection_ui_auto_open = self.get(|state| state.selection_ui_auto_open)?;
+ match syms {
+ Some(lsp_types::DocumentSymbolResponse::Flat(flat)) => {
+ self.present_list(&title, &flat)?;
+ }
+ Some(lsp_types::DocumentSymbolResponse::Nested(nested)) => {
+ let mut symbols = Vec::new();
- match selection_ui {
- SelectionUI::FZF => {
- let symbols = match syms {
- Some(lsp_types::DocumentSymbolResponse::Flat(flat)) => flat
- .iter()
- .map(|sym| {
- let start = sym.location.range.start;
- format!(
- "{}:{}:\t{}\t\t{:?}",
- start.line + 1,
- start.character + 1,
- sym.name,
- sym.kind
- )
- })
- .collect(),
- Some(lsp_types::DocumentSymbolResponse::Nested(nested)) => {
- let mut symbols = Vec::new();
+ fn walk_document_symbol(
+ buffer: &mut Vec<lsp_types::DocumentSymbol>,
+ parent: Option<&lsp_types::DocumentSymbol>,
+ ds: &lsp_types::DocumentSymbol,
+ ) {
+ let name = if let Some(parent) = parent {
+ format!("{}::{}", parent.name, ds.name)
+ } else {
+ ds.name.clone()
+ };
- fn walk_document_symbol(
- buffer: &mut Vec<String>,
- parent: Option<&str>,
- ds: &lsp_types::DocumentSymbol,
- ) {
- let start = ds.selection_range.start;
+ let n = lsp_types::DocumentSymbol {
+ name: name,
+ ..ds.clone()
+ };
- let name = if let Some(parent) = parent {
- format!("{}::{}", parent, ds.name)
- } else {
- ds.name.clone()
- };
+ buffer.push(n);
- let n = format!(
- "{}:{}:\t{}\t\t{:?}",
- start.line + 1,
- start.character + 1,
- name,
- ds.kind
- );
-
- buffer.push(n);
-
- if let Some(children) = &ds.children {
- for child in children {
- walk_document_symbol(buffer, Some(&ds.name), child);
- }
- }
+ if let Some(children) = &ds.children {
+ for child in children {
+ walk_document_symbol(buffer, Some(&ds), child);
}
-
- for ds in &nested {
- walk_document_symbol(&mut symbols, None, ds);
- }
-
- symbols
}
- _ => Vec::new(),
- };
-
- self.vim()?.rpcclient.notify(
- "s:FZF",
- json!([symbols, format!("s:{}", NOTIFICATION_FZF_SINK_LOCATION)]),
- )?;
- }
- SelectionUI::Quickfix => {
- let list = match syms {
- Some(lsp_types::DocumentSymbolResponse::Flat(flat)) => {
- flat.iter().map(QuickfixEntry::from_lsp).collect()
- }
- Some(lsp_types::DocumentSymbolResponse::Nested(nested)) => {
- <Vec<QuickfixEntry>>::from_lsp(&nested)
- }
- _ => Ok(Vec::new()),
- };
-
- let list = list?;
- self.vim()?.setqflist(&list, " ", &title)?;
- if selection_ui_auto_open {
- self.vim()?.command("botright copen")?;
}
- self.vim()?
- .echo("Document symbols populated to quickfix list.")?;
- }
- SelectionUI::LocationList => {
- let list = match syms {
- Some(lsp_types::DocumentSymbolResponse::Flat(flat)) => {
- flat.iter().map(QuickfixEntry::from_lsp).collect()
- }
- Some(lsp_types::DocumentSymbolResponse::Nested(nested)) => {
- <Vec<QuickfixEntry>>::from_lsp(&nested)
- }
- _ => Ok(Vec::new()),
- };
- let list = list?;
- self.vim()?.setloclist(&list, " ", &title)?;
- if selection_ui_auto_open {
- self.vim()?.command("lopen")?;
+ for ds in &nested {
+ walk_document_symbol(&mut symbols, None, ds);
}
- self.vim()?
- .echo("Document symbols populated to location list.")?;
+
+ self.present_list(&title, &symbols)?;
}
- }
+ _ => (),
+ };
info!("End {}", lsp_types::request::DocumentSymbolRequest::METHOD);
Ok(result)
diff --git i/src/types.rs w/src/types.rs
index 38d9640..66cca5b 100644
--- i/src/types.rs
+++ w/src/types.rs
@@ -1180,9 +1180,45 @@ impl ListItem for Command {
}
}
+impl ListItem for lsp_types::DocumentSymbol {
+ fn quickfix_item(&self, _: &LanguageClient) -> Result<QuickfixEntry> {
+ let start = self.selection_range.start;
+ let result = QuickfixEntry {
+ filename: "".to_string(),
+ lnum: start.line + 1,
+ col: Some(start.character + 1),
+ text: Some(self.name.clone()),
+ nr: None,
+ typ: None,
+ };
+ Ok(result)
+ }
+
+ fn string_item(&self, _: &LanguageClient, _: &str) -> Result<String> {
+ let start = self.selection_range.start;
+ let result = format!(
+ "{}:{}:\t{}\t\t{:?}",
+ start.line + 1,
+ start.character + 1,
+ self.name.clone(),
+ self.kind
+ );
+ Ok(result)
+ }
+}
+
impl ListItem for SymbolInformation {
fn quickfix_item(&self, _: &LanguageClient) -> Result<QuickfixEntry> {
- QuickfixEntry::from_lsp(self)
+ let start = self.location.range.start;
+
+ Ok(QuickfixEntry {
+ filename: self.location.uri.filepath()?.to_string_lossy().into_owned(),
+ lnum: start.line + 1,
+ col: Some(start.character + 1),
+ text: Some(self.name.clone()),
+ nr: None,
+ typ: None,
+ })
}
fn string_item(&self, _: &LanguageClient, cwd: &str) -> Result<String> {
@@ -1200,75 +1236,6 @@ impl ListItem for SymbolInformation {
}
}
-pub trait FromLSP<F>
-where
- Self: Sized,
-{
- fn from_lsp(f: &F) -> Result<Self>;
-}
-
-impl FromLSP<SymbolInformation> for QuickfixEntry {
- fn from_lsp(sym: &SymbolInformation) -> Result<Self> {
- let start = sym.location.range.start;
-
- Ok(Self {
- filename: sym.location.uri.filepath()?.to_string_lossy().into_owned(),
- lnum: start.line + 1,
- col: Some(start.character + 1),
- text: Some(sym.name.clone()),
- nr: None,
- typ: None,
- })
- }
-}
-
-impl FromLSP<Vec<lsp_types::SymbolInformation>> for Vec<QuickfixEntry> {
- fn from_lsp(symbols: &Vec<lsp_types::SymbolInformation>) -> Result<Self> {
- symbols.iter().map(QuickfixEntry::from_lsp).collect()
- }
-}
-
-impl FromLSP<Vec<lsp_types::DocumentSymbol>> for Vec<QuickfixEntry> {
- fn from_lsp(document_symbols: &Vec<lsp_types::DocumentSymbol>) -> Result<Self> {
- let mut symbols = Vec::new();
-
- fn walk_document_symbol(
- buffer: &mut Vec<QuickfixEntry>,
- parent: Option<&str>,
- ds: &lsp_types::DocumentSymbol,
- ) {
- let start = ds.selection_range.start;
-
- let name = if let Some(parent) = parent {
- format!("{}::{}", parent, ds.name)
- } else {
- ds.name.clone()
- };
-
- buffer.push(QuickfixEntry {
- filename: "".to_string(),
- lnum: start.line + 1,
- col: Some(start.character + 1),
- text: Some(name),
- nr: None,
- typ: None,
- });
-
- if let Some(children) = &ds.children {
- for child in children {
- walk_document_symbol(buffer, Some(&ds.name), child);
- }
- }
- }
-
- for ds in document_symbols {
- walk_document_symbol(&mut symbols, None, ds);
- }
-
- Ok(symbols)
- }
-}
-
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RawMessage {
@runiq
Copy link
Author

runiq commented Jun 23, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment