Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
matoous committed Dec 13, 2023
1 parent 6740e5b commit 113700e
Showing 1 changed file with 64 additions and 73 deletions.
137 changes: 64 additions & 73 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1138,79 +1138,8 @@ impl Application {
let language_server = language_server!();
let offset_encoding = language_server.offset_encoding();

let success = match params {
lsp::ShowDocumentParams {
external: Some(true),
uri,
..
} => {
self.jobs.callback(crate::open_external_url_callback(uri));
true
}
lsp::ShowDocumentParams {
uri,
selection,
take_focus,
..
} => {
match uri.to_file_path() {
Ok(path) => {
let action = match take_focus {
Some(true) => helix_view::editor::Action::Replace,
_ => helix_view::editor::Action::VerticalSplit,
};

match self.editor.open(&path, action) {
Ok(id) => {
let doc = doc_mut!(self.editor, &id);
if let Some(range) = selection {
// TODO: convert inside server
if let Some(new_range) = lsp_range_to_range(
doc.text(),
range,
offset_encoding,
) {
let view = view_mut!(self.editor);

// we flip the range so that the cursor sits on the start of the symbol
// (for example start of the function).
doc.set_selection(
view.id,
Selection::single(
new_range.head,
new_range.anchor,
),
);
if action.align_view(view, doc.id()) {
align_view(doc, view, Align::Center);
}
} else {
log::warn!(
"lsp position out of bounds - {:?}",
range
);
};
};
true
}
Err(err) => {
log::error!(
"failed to open path: {:?}: {:?}",
uri,
err
);
false
}
}
}
Err(err) => {
log::error!("unsupported file URI: {}: {:?}", uri, err);
false
}
}
}
};
Ok(json!(lsp::ShowDocumentResult { success }))
let result = self.handle_show_document(params, offset_encoding);
Ok(json!(result))
}
};

Expand All @@ -1220,6 +1149,68 @@ impl Application {
}
}

fn handle_show_document(
&mut self,
params: lsp::ShowDocumentParams,
offset_encoding: helix_lsp::OffsetEncoding,
) -> lsp::ShowDocumentResult {
if let lsp::ShowDocumentParams {
external: Some(true),
uri,
..
} = params
{
self.jobs.callback(crate::open_external_url_callback(uri));
return lsp::ShowDocumentResult { success: true };
};

let lsp::ShowDocumentParams {
uri,
selection,
take_focus,
..
} = params;

let path = match uri.to_file_path() {
Ok(path) => path,
Err(err) => {
log::error!("unsupported file URI: {}: {:?}", uri, err);
return lsp::ShowDocumentResult { success: false };
}
};

let action = match take_focus {
Some(true) => helix_view::editor::Action::Replace,
_ => helix_view::editor::Action::VerticalSplit,
};

let doc_id = match self.editor.open(&path, action) {
Ok(id) => id,
Err(err) => {
log::error!("failed to open path: {:?}: {:?}", uri, err);
return lsp::ShowDocumentResult { success: false };
}
};

let doc = doc_mut!(self.editor, &doc_id);
if let Some(range) = selection {
// TODO: convert inside server
if let Some(new_range) = lsp_range_to_range(doc.text(), range, offset_encoding) {
let view = view_mut!(self.editor);

// we flip the range so that the cursor sits on the start of the symbol
// (for example start of the function).
doc.set_selection(view.id, Selection::single(new_range.head, new_range.anchor));
if action.align_view(view, doc.id()) {
align_view(doc, view, Align::Center);
}
} else {
log::warn!("lsp position out of bounds - {:?}", range);
};
};
lsp::ShowDocumentResult { success: true }
}

async fn claim_term(&mut self) -> std::io::Result<()> {
let terminal_config = self.config.load().editor.clone().into();
self.terminal.claim(terminal_config)
Expand Down

0 comments on commit 113700e

Please sign in to comment.