Skip to content

Commit

Permalink
1. Terminal file jump: change the judgment point to Ctrl + left mouse…
Browse files Browse the repository at this point in the history
… click.

2. Optimize some code according to clippy.
  • Loading branch information
jm-observer committed Jun 27, 2024
1 parent db55cbe commit d9d27e8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 46 deletions.
5 changes: 4 additions & 1 deletion lapce-app/src/terminal/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ impl RawTerminal {
proxy: ProxyRpcHandler,
term_notification_tx: Sender<TermNotification>,
) -> Self {
let config = alacritty_terminal::term::Config::default();
let config = alacritty_terminal::term::Config {
semantic_escape_chars: ",│`|\"' ()[]{}<>\t".to_string(),
..Default::default()
};
let event_proxy = EventProxy {
term_id,
proxy,
Expand Down
85 changes: 41 additions & 44 deletions lapce-app/src/terminal/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alacritty_terminal::{
grid::Dimensions,
index::Side,
selection::{Selection, SelectionType},
term::{cell::Flags, search::Match, test::TermSize, RenderableContent},
term::{cell::Flags, test::TermSize, RenderableContent},
};
use floem::{
context::{EventCx, PaintCx},
Expand All @@ -23,6 +23,7 @@ use lapce_core::mode::Mode;
use lapce_rpc::{proxy::ProxyRpcHandler, terminal::TermId};
use lsp_types::Position;
use parking_lot::RwLock;
use regex::Regex;
use unicode_width::UnicodeWidthChar;

use super::{panel::TerminalPanelData, raw::RawTerminal};
Expand Down Expand Up @@ -67,7 +68,7 @@ pub struct TerminalView {
launch_error: RwSignal<Option<String>>,
internal_command: Listener<InternalCommand>,
workspace: Arc<LapceWorkspace>,
hyper_matches: Vec<Match>,
hyper_regs: Vec<Regex>,
previous_mouse_action: MouseAction,
current_mouse_action: MouseAction,
}
Expand Down Expand Up @@ -122,6 +123,9 @@ pub fn terminal_view(
is_focused
});

// for rust
let reg = regex::Regex::new("[\\w\\\\/]+\\.rs:\\d+:\\d+").unwrap();

TerminalView {
id,
term_id,
Expand All @@ -135,7 +139,7 @@ pub fn terminal_view(
launch_error,
internal_command,
workspace,
hyper_matches: vec![],
hyper_regs: vec![reg],
previous_mouse_action: Default::default(),
current_mouse_action: Default::default(),
}
Expand Down Expand Up @@ -165,25 +169,40 @@ impl TerminalView {
}

fn click(&self, pos: Point) -> Option<()> {
let raw_origin = self.raw.read();
let raw = self.raw.read();
let position = self.get_terminal_point(pos);
let hy = self.hyper_matches.iter().find(|x| x.contains(&position))?;
let hyperlink = raw_origin.term.bounds_to_string(*hy.start(), *hy.end());
let content: Vec<&str> = hyperlink.split(':').collect();
let (file, line_str, col_str) =
(content.first()?, content.get(1)?, content.get(2)?);
let (line, col) =
(line_str.parse::<u32>().ok()?, col_str.parse::<u32>().ok()?);
let parent_path = self.workspace.path.as_ref()?;
self.internal_command.send(InternalCommand::JumpToLocation {
location: EditorLocation {
path: parent_path.join(file),
position: Some(EditorPosition::Position(Position::new(line, col))),
scroll_offset: None,
ignore_unconfirmed: false,
same_editor_tab: false,
},
});
let start_point = raw.term.semantic_search_left(position);
let end_point = raw.term.semantic_search_right(position);
let mut selection =
Selection::new(SelectionType::Simple, start_point, Side::Left);
selection.update(end_point, Side::Right);
selection.include_all();
if let Some(selection) = selection.to_range(&raw.term) {
let content = raw.term.bounds_to_string(selection.start, selection.end);
if let Some(match_str) =
self.hyper_regs.iter().find_map(|x| x.find(&content))
{
let hyperlink = match_str.as_str();
let content: Vec<&str> = hyperlink.split(':').collect();
let (file, line_str, col_str) =
(content.first()?, content.get(1)?, content.get(2)?);
let (line, col) =
(line_str.parse::<u32>().ok()?, col_str.parse::<u32>().ok()?);
let parent_path = self.workspace.path.as_ref()?;
self.internal_command.send(InternalCommand::JumpToLocation {
location: EditorLocation {
path: parent_path.join(file),
position: Some(EditorPosition::Position(Position::new(
line, col,
))),
scroll_offset: None,
ignore_unconfirmed: false,
same_editor_tab: false,
},
});
return Some(());
}
}
None
}

Expand Down Expand Up @@ -304,7 +323,6 @@ impl TerminalView {
config: &LapceConfig,
) {
let term_bg = config.color(LapceColor::TERMINAL_BACKGROUND);
let term_fg = config.color(LapceColor::TERMINAL_FOREGROUND);

let font_size = config.terminal_font_size();
let font_family = config.terminal_font_family();
Expand Down Expand Up @@ -332,9 +350,6 @@ impl TerminalView {
let y =
(point.line.0 as f64 + content.display_offset as f64) * line_height;
let char_y = y + (line_height - char_size.height) / 2.0;
let underline_y = (point.line.0 as f64 + content.display_offset as f64)
* line_height
+ line_height;
if y != line_content.y {
self.paint_line_content(
cx,
Expand Down Expand Up @@ -377,24 +392,6 @@ impl TerminalView {
}
}

if self.hyper_matches.iter().any(|x| x.contains(&point)) {
let mut extend = false;
if let Some((_, end, _, _)) = line_content.underline.last_mut() {
if *end == point.column.0 {
*end += 1;
extend = true;
}
}
if !extend {
line_content.underline.push((
point.column.0,
point.column.0 + 1,
term_fg,
underline_y,
));
}
}

if cursor_point == &point {
line_content.cursor = Some((cell.c, x));
}
Expand Down Expand Up @@ -501,7 +498,7 @@ impl View for TerminalView {
match self.current_mouse_action {
MouseAction::LeftOnce { pos, .. } => {
clear_selection = true;
if self.click(pos).is_some() {
if e.modifiers.control() && self.click(pos).is_some() {
return EventPropagation::Stop;
}
}
Expand Down
2 changes: 1 addition & 1 deletion lapce-core/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,11 +1054,11 @@ impl LapceLanguage {
}

fn get_grammar(&self) -> Option<tree_sitter::Language> {
let props = self.properties();
let grammar_name = self.grammar_name();

#[cfg(unix)]
{
let props = self.properties();
let grammars_dir = Path::new(Self::SYSTEM_GRAMMARS_DIRECTORY);
if grammars_dir.exists() {
let grammars_dir = grammars_dir.join(props.id.as_ref());
Expand Down

0 comments on commit d9d27e8

Please sign in to comment.