Skip to content

Commit

Permalink
feat: disable inlay hints on one line content blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Myriad-Dreamin committed Mar 15, 2024
1 parent f683426 commit 70e6350
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#let add(x, y) = x + y

#add[][]

#add[
][
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
source: crates/tinymist-query/src/inlay_hint.rs
expression: "JsonRepr::new_redacted(result, &REDACT_LOC)"
input_file: crates/tinymist-query/src/fixtures/inlay_hints/one_line_content.typ
---
[
{
"kind": 2,
"label": ": x",
"position": {
"character": 1,
"line": 5
}
},
{
"kind": 2,
"label": ": y",
"position": {
"character": 1,
"line": 6
}
}
]
37 changes: 36 additions & 1 deletion crates/tinymist-query/src/inlay_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,28 @@ fn inlay_hint(
(false, false)
};

let disable_by_single_line_content_block = !SMART.on_content_block_args
|| 'one_line: {
for arg in args.items() {
let Some(arg_node) = args_node.find(arg.span()) else {
continue;
};

let Some(info) = call_info.arg_mapping.get(&arg_node) else {
continue;
};

if info.kind != ParamKind::Named
&& info.is_content_block
&& !is_one_line(self.source, &arg_node)
{
break 'one_line false;
}
}

true
};

let mut is_first_variadic_arg = true;

for arg in args.items() {
Expand Down Expand Up @@ -208,7 +230,8 @@ fn inlay_hint(
ParamKind::Positional
if !SMART.on_pos_args
|| (info.is_content_block
&& disable_by_single_content_pos_arg)
&& (disable_by_single_content_pos_arg
|| disable_by_single_line_content_block))
|| (!info.is_content_block && disable_by_single_pos_arg) =>
{
continue
Expand Down Expand Up @@ -621,6 +644,18 @@ fn analyze_closure_signature(c: Arc<LazyHash<Closure>>) -> Vec<Arc<ParamSpec>> {
params
}

fn is_one_line(src: &Source, arg_node: &LinkedNode<'_>) -> bool {
is_one_line_(src, arg_node).unwrap_or(true)
}

fn is_one_line_(src: &Source, arg_node: &LinkedNode<'_>) -> Option<bool> {
let lb = arg_node.children().next()?;
let rb = arg_node.children().next_back()?;
let ll = src.byte_to_line(lb.offset())?;
let rl = src.byte_to_line(rb.offset())?;
Some(ll == rl)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 70e6350

Please sign in to comment.