Skip to content
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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ Jumps to various locations.
| `g` | Go to line number `<n>` else start of file | `goto_file_start` |
| `e` | Go to the end of the file | `goto_last_line` |
| `f` | Go to files in the selection | `goto_file` |
| `u` | Go to url in the selection | `goto_url` |
Copy link
Member

@dead10ck dead10ck Oct 21, 2022

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.

| `h` | Go to the start of the line | `goto_line_start` |
| `l` | Go to the end of the line | `goto_line_end` |
| `s` | Go to first non-whitespace character of the line | `goto_first_nonwhitespace` |
Expand Down
31 changes: 31 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is actually a range; could we change this name to range?

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),
Copy link
Member

Choose a reason for hiding this comment

The 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) {
Copy link
Member

Choose a reason for hiding this comment

The 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,
Expand Down
1 change: 1 addition & 0 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn default() -> HashMap<Mode, Keymap> {
"g" => goto_file_start,
"e" => goto_last_line,
"f" => goto_file,
"u" => goto_url,
"h" => goto_line_start,
"l" => goto_line_end,
"s" => goto_first_nonwhitespace,
Expand Down
2 changes: 2 additions & 0 deletions helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ log = "~0.4"

which = "4.2"

open = "3"
Copy link
Member

Choose a reason for hiding this comment

The 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"] }

Expand Down
6 changes: 6 additions & 0 deletions helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,12 @@ impl Editor {
self._refresh();
}

pub fn open_url(&mut self, url: lsp::Url) {
Copy link
Member

Choose a reason for hiding this comment

The 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 Editor function.

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,
Expand Down