Skip to content

Commit

Permalink
POC: send inferred types to client
Browse files Browse the repository at this point in the history
  • Loading branch information
phiresky committed Jul 18, 2018
1 parent 52db3cd commit d2565c2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
35 changes: 26 additions & 9 deletions src/actions/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ use rls_span as span;

use crate::actions::work_pool;
use crate::actions::work_pool::WorkDescription;
use crate::actions::ActionContext;
use crate::actions::run::collect_run_actions;
use crate::lsp_data;
use crate::lsp_data::*;
use crate::server;
use crate::server::{Ack, Output, Request, RequestAction, ResponseError};

use crate::server::{Ack, Output, Request, RequestAction, BlockingRequestAction, ResponseError, RequestId};
use jsonrpc_core::types::ErrorCode;
use rls_analysis::SymbolQuery;
use rls_analysis::{SymbolQuery, DefKind};
use crate::server::{Notification};

use crate::lsp_data::request::ApplyWorkspaceEdit;
pub use crate::lsp_data::request::{
Expand Down Expand Up @@ -98,23 +101,37 @@ impl RequestAction for WorkspaceSymbol {
}
}

impl RequestAction for Symbols {
// TODO: how would you send a notification from a RequestAction asynchronously (without blocking?)
impl BlockingRequestAction for Symbols {
type Response = Vec<SymbolInformation>;

fn fallback_response() -> Result<Self::Response, ResponseError> {
Ok(vec![])
}

fn handle(
ctx: InitActionContext,
fn handle<O: Output>(
_id: RequestId,
params: Self::Params,
ctx: &mut ActionContext,
output: O
) -> Result<Self::Response, ResponseError> {
let ctx = ctx.inited().unwrap();
let analysis = ctx.analysis;

let file_path = parse_file_path!(&params.text_document.uri, "symbols")?;

let symbols = analysis.symbols(&file_path).unwrap_or_else(|_| vec![]);

let symbol_types: Vec<_> = symbols.iter()
.filter(|s| s.kind == DefKind::Local)
.filter_map(|s| {
// info!("DEF {:?}: {:?}", s, analysis.get_def(s.id));
analysis.get_def(s.id).ok().map(|r| SymbolInferredTypeInformation {
name: s.name.clone(),
location: ls_util::rls_to_location(&s.span),
inferred_type: r.value.clone()
})
}).collect();

output.notify(Notification::<InferredTypesNotification>::new(InferredTypesParams {
list: symbol_types
}));
Ok(
symbols
.into_iter()
Expand Down
20 changes: 20 additions & 0 deletions src/lsp_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ impl LSPNotification for BeginBuild {
const METHOD: &'static str = "rustDocument/beginBuild";
}

#[derive(Debug)]
pub enum InferredTypesNotification { }

impl LSPNotification for InferredTypesNotification {
type Params = InferredTypesParams;
const METHOD: &'static str = "rustDocument/inferredSymbolTypes";
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
pub struct InferredTypesParams {
pub list: Vec<SymbolInferredTypeInformation>
}

#[derive(Debug, PartialEq, Deserialize, Serialize, Clone)]
pub struct SymbolInferredTypeInformation {
pub name: String,
pub location: Location,
pub inferred_type: String
}

/* ---------- Temporary LSP type until window/progress proposal is done --------- */

// Notification from server to client for build progress.
Expand Down
1 change: 0 additions & 1 deletion src/server/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ define_dispatch_request_enum!(
Definition,
References,
WorkspaceSymbol,
Symbols,
Hover,
Implementation,
DocumentHighlight,
Expand Down
2 changes: 1 addition & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ impl<O: Output> LsService<O> {
notifications::Cancel;
blocking_requests:
ShutdownRequest,
requests::Symbols,
InitializeRequest;
requests:
requests::ExecuteCommand,
Expand All @@ -271,7 +272,6 @@ impl<O: Output> LsService<O> {
requests::CodeAction,
requests::DocumentHighlight,
requests::Implementation,
requests::Symbols,
requests::Hover,
requests::WorkspaceSymbol,
requests::Definition,
Expand Down

0 comments on commit d2565c2

Please sign in to comment.