-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add goto_url (gu) command #4398
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -291,6 +291,7 @@ impl MappableCommand { | |
goto_file, "Goto files in selection", | ||
goto_file_hsplit, "Goto files in selection (hsplit)", | ||
goto_file_vsplit, "Goto files in selection (vsplit)", | ||
goto_url, "Goto url in selection", | ||
goto_reference, "Goto references", | ||
goto_window_top, "Goto window top", | ||
goto_window_center, "Goto window center", | ||
|
@@ -1052,6 +1053,36 @@ fn goto_file_impl(cx: &mut Context, action: Action) { | |
} | ||
} | ||
|
||
fn goto_url(cx: &mut Context) { | ||
use helix_lsp::Url; | ||
|
||
let (view, doc) = current_ref!(cx.editor); | ||
let selection = doc.selection(view.id).primary(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually a range; could we change this name to |
||
let text = doc.text(); | ||
|
||
let text = if selection.to() - selection.from() == 1 { | ||
let current_word = movement::move_next_long_word_start( | ||
text.slice(..), | ||
movement::move_prev_long_word_start(text.slice(..), selection, 1), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be a little clearer if this were in its own variable. |
||
1, | ||
); | ||
text.slice(current_word.from()..current_word.to()) | ||
.to_string() | ||
} else { | ||
text.slice(selection.from()..selection.to()).to_string() | ||
}; | ||
|
||
match Url::parse(&text) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason to parse a URL? What happens if you run this command with some arbitrary text? |
||
Ok(url) => { | ||
cx.editor.open_url(url); | ||
} | ||
Err(e) => { | ||
cx.editor | ||
.set_error(format!("Failed to parse url '{}': {}", text, e.to_string())); | ||
} | ||
} | ||
} | ||
|
||
fn extend_word_impl<F>(cx: &mut Context, extend_fn: F) | ||
where | ||
F: Fn(RopeSlice, Range, usize) -> Range, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,8 @@ log = "~0.4" | |
|
||
which = "4.2" | ||
|
||
open = "3" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The maintainers might be wary of pulling in a crate for just this command. |
||
|
||
[target.'cfg(windows)'.dependencies] | ||
clipboard-win = { version = "4.4", features = ["std"] } | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1079,6 +1079,12 @@ impl Editor { | |
self._refresh(); | ||
} | ||
|
||
pub fn open_url(&mut self, url: lsp::Url) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could probably just all go into the command. I'm not sure I understand why this is an |
||
if let Err(e) = open::that(url.as_str()) { | ||
self.set_error(format!("Failed to open url '{}': {:?}", url.as_str(), e)); | ||
} | ||
} | ||
|
||
pub fn close_document(&mut self, doc_id: DocumentId, force: bool) -> Result<(), CloseError> { | ||
let doc = match self.documents.get(&doc_id) { | ||
Some(doc) => doc, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be clearer if this explicitly mentioned that it will open the URL in a web browser.