Skip to content

Commit

Permalink
return multiple positions from jump_to_cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Song-Nop committed Dec 13, 2024
1 parent b19810e commit 3e82701
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 49 deletions.
60 changes: 31 additions & 29 deletions crates/tinymist/src/tool/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,32 @@ impl CompileHandler {
async fn resolve_document_position(
snap: &SucceededArtifact<LspCompilerFeat>,
loc: Location,
) -> Option<Position> {
) -> Vec<Position> {
let Location::Src(src_loc) = loc;

let path = Path::new(&src_loc.filepath).to_owned();
let line = src_loc.pos.line;
let column = src_loc.pos.column;

let doc = snap.success_doc();
let doc = doc.as_deref()?;
let Some(doc) = doc.as_deref() else {
return vec![];
};
let world = snap.world();

let relative_path = path.strip_prefix(&world.workspace_root()?).ok()?;
let Some(root) = world.workspace_root() else {
return vec![];
};
let Some(relative_path) = path.strip_prefix(root).ok() else {
return vec![];
};

let source_id = TypstFileId::new(None, VirtualPath::new(relative_path));
let source = world.source(source_id).ok()?;
let cursor = source.line_column_to_byte(line, column)?;
let Some(source) = world.source(source_id).ok() else {
return vec![];
};
let Some(cursor) = source.line_column_to_byte(line, column) else {
return vec![];
};

jump_from_cursor(doc, &source, cursor)
}
Expand Down Expand Up @@ -115,7 +125,7 @@ impl SourceFileServer for CompileHandler {

/// fixme: character is 0-based, UTF-16 code unit.
/// We treat it as UTF-8 now.
async fn resolve_document_position(&self, loc: Location) -> Result<Option<Position>, Error> {
async fn resolve_document_position(&self, loc: Location) -> Result<Vec<Position>, Error> {
let snap = self.artifact()?.receive().await?;
Ok(Self::resolve_document_position(&snap, loc).await)
}
Expand Down Expand Up @@ -675,38 +685,30 @@ impl Notification for NotifDocumentOutline {
}

/// Find the output location in the document for a cursor position.
fn jump_from_cursor(document: &TypstDocument, source: &Source, cursor: usize) -> Option<Position> {
let node = LinkedNode::new(source.root()).leaf_at_compat(cursor)?;
if node.kind() != SyntaxKind::Text {
return None;
}
fn jump_from_cursor(document: &TypstDocument, source: &Source, cursor: usize) -> Vec<Position> {
let Some(node) = LinkedNode::new(source.root())
.leaf_at_compat(cursor)
.filter(|node| node.kind() == SyntaxKind::Text)
else {
return vec![];
};

let mut min_dis = u64::MAX;
let mut p = Point::default();
let mut ppage = 0usize;

let span = node.span();
let mut positions: Vec<Position> = vec![];
for (i, page) in document.pages.iter().enumerate() {
let t_dis = min_dis;
let mut min_dis = u64::MAX;
if let Some(pos) = find_in_frame(&page.frame, span, &mut min_dis, &mut p) {
return Some(Position {
page: NonZeroUsize::new(i + 1)?,
point: pos,
});
}
if t_dis != min_dis {
ppage = i;
if let Some(page) = NonZeroUsize::new(i + 1) {
positions.push(Position { page, point: pos });
}
}
}

if min_dis == u64::MAX {
return None;
}
log::info!("jump_from_cursor: {positions:#?}");

Some(Position {
page: NonZeroUsize::new(ppage + 1)?,
point: p,
})
positions
}

/// Find the position of a span in a frame.
Expand Down
16 changes: 3 additions & 13 deletions crates/typst-preview/src/actor/typst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,16 @@ impl<T: SourceFileServer + EditorServer> TypstActor<T> {
.map_err(|err| {
error!("TypstActor: failed to resolve src to doc jump: {:#}", err);
})
.ok()
.flatten();
// impl From<TypstPosition> for DocumentPosition {
// fn from(position: TypstPosition) -> Self {
// Self {
// page_no: position.page.into(),
// x: position.point.x.to_pt() as f32,
// y: position.point.y.to_pt() as f32,
// }
// }
// }
.ok();

if let Some(info) = res {
let _ = self
.webview_conn_sender
.send(WebviewActorRequest::SrcToDocJump(DocumentPosition {
.send(WebviewActorRequest::SrcToDocJump(info.into_iter().map(|info| DocumentPosition {
page_no: info.page.into(),
x: info.point.x.to_pt() as f32,
y: info.point.y.to_pt() as f32,
}));
}).collect()));
}
}
TypstActorRequest::SyncMemoryFiles(m) => {
Expand Down
16 changes: 14 additions & 2 deletions crates/typst-preview/src/actor/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub type SrcToDocJumpInfo = DocumentPosition;
#[derive(Debug, Clone)]
pub enum WebviewActorRequest {
ViewportPosition(DocumentPosition),
SrcToDocJump(SrcToDocJumpInfo),
SrcToDocJump(Vec<SrcToDocJumpInfo>),
// CursorPosition(CursorPosition),
CursorPaths(Vec<Vec<ElementPoint>>),
}
Expand All @@ -29,6 +29,18 @@ fn position_req(
format!("{event},{page_no} {x} {y}")
}

fn positions_req(
event: &'static str,
positions: Vec<DocumentPosition>,
) -> String {
format!("{event},")
+ &positions
.iter()
.map(|DocumentPosition { page_no, x, y }| format!("{page_no} {x} {y}"))
.collect::<Vec<_>>()
.join(",")
}

pub struct WebviewActor<
'a,
C: futures::Sink<Message, Error = WsError> + futures::Stream<Item = Result<Message, WsError>>,
Expand Down Expand Up @@ -84,7 +96,7 @@ impl<
trace!("WebviewActor: received message from mailbox: {:?}", msg);
match msg {
WebviewActorRequest::SrcToDocJump(jump_info) => {
let msg = position_req("jump", jump_info);
let msg = positions_req("jump", jump_info);
self.webview_websocket_conn.send(Message::Binary(msg.into_bytes()))
.await.unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions crates/typst-preview/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ pub trait SourceFileServer {
fn resolve_document_position(
&self,
_by: Location,
) -> impl Future<Output = Result<Option<Position>, Error>> + Send {
async { Ok(None) }
) -> impl Future<Output = Result<Vec<Position>, Error>> + Send {
async { Ok(vec![]) }
}

fn resolve_source_location(
Expand Down
19 changes: 16 additions & 3 deletions tools/typst-preview-frontend/src/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,23 @@ export async function wsMain({ url, previewMode, isContentPreview }: WsArgs) {

if (message[0] === "jump" || message[0] === "viewport") {
// todo: aware height padding
const [page, x, y] = dec
const current_page_number = svgDoc.getPartialPageNumber();

let positions = dec
.decode((message[1] as any).buffer)
.split(" ")
.map(Number);
.split(",")

// choose the page, x, y closest to the current page
const [page, x, y] = positions.reduce((acc, cur) => {
const [page, x, y] = cur.split(" ").map(Number);
console.log("jump", page, x, y);
alert("jump " + page + " " + x + " " + y);
const current_page = current_page_number;
if (Math.abs(page - current_page) < Math.abs(acc[0] - current_page)) {
return [page, x, y];
}
return acc;
}, [Number.MAX_SAFE_INTEGER, 0, 0]);

let pageToJump = page;

Expand Down

0 comments on commit 3e82701

Please sign in to comment.