Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit da674a2

Browse files
authored
Merge pull request #587 from alexheretic/more-nonblocking-requests
Add nonblocking Formatting, RangeFormatting, ResolveCompletion
2 parents 05ca47c + be581db commit da674a2

File tree

4 files changed

+63
-60
lines changed

4 files changed

+63
-60
lines changed

src/actions/requests.rs

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -764,21 +764,26 @@ impl Action for Formatting {
764764
const METHOD: &'static str = "textDocument/formatting";
765765
}
766766

767-
impl<'a> BlockingRequestAction<'a> for Formatting {
767+
impl RequestAction for Formatting {
768768
type Response = [TextEdit; 1];
769769

770-
fn new(_: &'a mut LsState) -> Self {
770+
fn new() -> Self {
771771
Formatting
772772
}
773773

774-
fn handle<O: Output>(
774+
fn fallback_response(&self) -> Result<Self::Response, ResponseError> {
775+
Err(ResponseError::Message(
776+
ErrorCode::InternalError,
777+
"Reformat failed to complete successfully".into(),
778+
))
779+
}
780+
781+
fn handle(
775782
&mut self,
776-
id: usize,
783+
ctx: InitActionContext,
777784
params: Self::Params,
778-
ctx: &mut ActionContext,
779-
out: O,
780-
) -> Result<Self::Response, ()> {
781-
reformat(id, params.text_document, None, &params.options, ctx, out)
785+
) -> Result<Self::Response, ResponseError> {
786+
reformat(params.text_document, None, &params.options, ctx)
782787
}
783788
}
784789

@@ -790,69 +795,64 @@ impl Action for RangeFormatting {
790795
const METHOD: &'static str = "textDocument/rangeFormatting";
791796
}
792797

793-
impl<'a> BlockingRequestAction<'a> for RangeFormatting {
798+
impl RequestAction for RangeFormatting {
794799
type Response = [TextEdit; 1];
795800

796-
fn new(_: &'a mut LsState) -> Self {
801+
fn new() -> Self {
797802
RangeFormatting
798803
}
799804

800-
fn handle<O: Output>(
805+
fn fallback_response(&self) -> Result<Self::Response, ResponseError> {
806+
Err(ResponseError::Message(
807+
ErrorCode::InternalError,
808+
"Reformat failed to complete successfully".into(),
809+
))
810+
}
811+
812+
fn handle(
801813
&mut self,
802-
id: usize,
814+
ctx: InitActionContext,
803815
params: Self::Params,
804-
ctx: &mut ActionContext,
805-
out: O,
806-
) -> Result<Self::Response, ()> {
816+
) -> Result<Self::Response, ResponseError> {
807817
reformat(
808-
id,
809818
params.text_document,
810819
Some(params.range),
811820
&params.options,
812821
ctx,
813-
out,
814822
)
815823
}
816824
}
817825

818-
fn reformat<O: Output>(
819-
id: usize,
826+
fn reformat(
820827
doc: TextDocumentIdentifier,
821828
selection: Option<Range>,
822829
opts: &FormattingOptions,
823-
ctx: &mut ActionContext,
824-
out: O,
825-
) -> Result<[TextEdit; 1], ()> {
830+
ctx: InitActionContext,
831+
) -> Result<[TextEdit; 1], ResponseError> {
826832
trace!(
827-
"Reformat: {} {:?} {:?} {} {}",
828-
id,
833+
"Reformat: {:?} {:?} {} {}",
829834
doc,
830835
selection,
831836
opts.tab_size,
832837
opts.insert_spaces
833838
);
834-
let ctx = ctx.inited();
835839
let path = parse_file_path!(&doc.uri, "reformat")?;
836840

837841
let input = match ctx.vfs.load_file(&path) {
838842
Ok(FileContents::Text(s)) => FmtInput::Text(s),
839843
Ok(_) => {
840844
debug!("Reformat failed, found binary file");
841-
out.failure_message(
842-
id,
845+
return Err(ResponseError::Message(
843846
ErrorCode::InternalError,
844-
"Reformat failed to complete successfully",
845-
);
846-
return Err(());
847+
"Reformat failed to complete successfully".into(),
848+
));
847849
}
848850
Err(e) => {
849851
debug!("Reformat failed: {:?}", e);
850-
out.failure_message(
851-
id,
852+
return Err(ResponseError::Message(
852853
ErrorCode::InternalError,
853-
"Reformat failed to complete successfully",
854-
);
855-
return Err(());
854+
"Reformat failed to complete successfully".into(),
855+
));
856856
}
857857
};
858858

@@ -900,22 +900,19 @@ fn reformat<O: Output>(
900900
summary
901901
);
902902

903-
out.failure_message(
904-
id,
903+
return Err(ResponseError::Message(
905904
ErrorCode::InternalError,
906-
"Reformat failed to complete successfully",
907-
);
908-
Err(())
905+
"Reformat failed to complete successfully".into(),
906+
));
909907
}
910908
}
911909
Err(e) => {
912910
debug!("Reformat failed: {:?}", e);
913-
out.failure_message(
914-
id,
911+
912+
return Err(ResponseError::Message(
915913
ErrorCode::InternalError,
916-
"Reformat failed to complete successfully",
917-
);
918-
Err(())
914+
"Reformat failed to complete successfully".into(),
915+
));
919916
}
920917
}
921918
}
@@ -931,24 +928,26 @@ impl Action for ResolveCompletion {
931928
const METHOD: &'static str = "completionItem/resolve";
932929
}
933930

934-
impl<'a> BlockingRequestAction<'a> for ResolveCompletion {
931+
impl RequestAction for ResolveCompletion {
935932
type Response = CompletionItem;
936933

937-
fn new(_: &'a mut LsState) -> Self {
934+
fn new() -> Self {
938935
ResolveCompletion
939936
}
940937

941-
fn handle<O: Output>(
938+
fn fallback_response(&self) -> Result<Self::Response, ResponseError> {
939+
Err(ResponseError::Empty)
940+
}
941+
942+
fn handle(
942943
&mut self,
943-
_id: usize,
944+
_: InitActionContext,
944945
params: Self::Params,
945-
_ctx: &mut ActionContext,
946-
_out: O,
947-
) -> Result<Self::Response, ()> {
946+
) -> Result<Self::Response, ResponseError> {
948947
// currently, we safely ignore this as a pass-through since we fully handle
949948
// textDocument/completion. In the future, we may want to use this method as a
950949
// way to more lazily fill out completion information
951-
Ok(params)
950+
Ok(params.into())
952951
}
953952
}
954953

src/server/dispatch.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ define_dispatch_request_enum!(
7676
FindImpls,
7777
DocumentHighlight,
7878
Rename,
79-
CodeAction
79+
CodeAction,
80+
ResolveCompletion,
81+
Formatting,
82+
RangeFormatting
8083
);
8184

8285
/// Provides ability to dispatch requests to a worker thread that will
@@ -163,6 +166,7 @@ pub trait RequestAction: Action {
163166
}
164167

165168
/// Wrapper for a response error
169+
#[derive(Debug)]
166170
pub enum ResponseError {
167171
/// Error with no special response to the client
168172
Empty,

src/server/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,11 @@ impl<O: Output> LsService<O> {
501501
blocking_requests:
502502
ShutdownRequest,
503503
InitializeRequest,
504-
requests::ResolveCompletion,
505-
requests::ExecuteCommand,
506-
requests::Formatting,
507-
requests::RangeFormatting;
504+
requests::ExecuteCommand;
508505
requests:
506+
requests::Formatting,
507+
requests::RangeFormatting,
508+
requests::ResolveCompletion,
509509
requests::Rename,
510510
requests::CodeAction,
511511
requests::DocumentHighlight,

src/test/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ fn test_reformat() {
552552
let text_doc = TextDocumentIdentifier::new(url);
553553
let messages = vec![
554554
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string(),
555-
blocking_request::<requests::Formatting>(
555+
request::<requests::Formatting>(
556556
42,
557557
DocumentFormattingParams {
558558
text_document: text_doc,
@@ -600,7 +600,7 @@ fn test_reformat_with_range() {
600600
let text_doc = TextDocumentIdentifier::new(url);
601601
let messages = vec![
602602
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string(),
603-
blocking_request::<requests::RangeFormatting>(
603+
request::<requests::RangeFormatting>(
604604
42,
605605
DocumentRangeFormattingParams {
606606
text_document: text_doc,

0 commit comments

Comments
 (0)