Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit 27d7fc1

Browse files
committed
Changes from #54
1 parent 5e37431 commit 27d7fc1

File tree

5 files changed

+102
-33
lines changed

5 files changed

+102
-33
lines changed

DEBUGGING.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# rust-lldb
2+
3+
https://dev.to/bmatcuk/debugging-rust-with-rust-lldb-j1f
4+
5+
Run `just test-debug` to run the tests in debug mode using lldb.
6+
7+
Now use `r <test_name>` to run a specific test. https://users.rust-lang.org/t/running-a-single-test-under-a-debugger/44460

Justfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,13 @@ test:
44

55
test-print test_name:
66
RUNNING_TESTS=true RUST_LOG=trace RUST_BACKTRACE=1 cargo test -- --test-threads=1 {{test_name}}
7+
8+
[macos]
9+
test-debug test_name breakpoint:
10+
#!/bin/bash
11+
TEST_OUTPUT=$(RUNNING_TESTS=true cargo test --no-run 2>&1 >/dev/null)
12+
DEP1=$(echo $TEST_OUTPUT | grep -ohe 'Executable tests/logseq/main.rs (target/debug/deps/logseq-[a-z0-9]*' | awk -F'[()]' '{print $2}')
13+
echo $DEP1
14+
RUNNING_TESTS=true RUST_LOG=debug RUST_BACKTRACE=full rust-lldb $DEP1 \
15+
-o "b {{breakpoint}}" \
16+
-o "r {{test_name}}"

bin/byte_index

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python3
2+
3+
import argparse
4+
5+
def find_byte_indexes(text, search_term):
6+
# Encode both the text and the search term into bytes
7+
byte_text = text.encode('utf-8')
8+
byte_search_term = search_term.encode('utf-8')
9+
10+
# Initialize a list to store the byte indexes
11+
byte_indexes = []
12+
13+
# Start searching for the term in the byte_text
14+
index = byte_text.find(byte_search_term)
15+
while index != -1:
16+
byte_indexes.append(index)
17+
# Continue searching after the current match
18+
index = byte_text.find(byte_search_term, index + 1)
19+
20+
return byte_indexes
21+
22+
def main():
23+
# Set up argument parser
24+
parser = argparse.ArgumentParser(description="Find byte indexes of a search term in a file.")
25+
parser.add_argument("file", help="Path to the file to be searched")
26+
parser.add_argument("search_term", help="The term to search for in the file")
27+
28+
# Parse the arguments
29+
args = parser.parse_args()
30+
31+
# Read the file
32+
try:
33+
with open(args.file, 'r', encoding='utf-8') as f:
34+
file_content = f.read()
35+
except FileNotFoundError:
36+
print(f"Error: File '{args.file}' not found.")
37+
return
38+
except Exception as e:
39+
print(f"Error reading file: {e}")
40+
return
41+
42+
# Find byte indexes
43+
indexes = find_byte_indexes(file_content, args.search_term)
44+
45+
# Print the results
46+
if indexes:
47+
print(f"Found '{args.search_term}' at byte indexes: {indexes}")
48+
else:
49+
print(f"'{args.search_term}' not found in the file.")
50+
51+
if __name__ == "__main__":
52+
main()

src/file/content/wikilink.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl Alias {
3434
self.0.is_empty()
3535
}
3636
#[must_use]
37-
pub fn len(&self) -> usize {
37+
pub fn char_len(&self) -> usize {
3838
self.0.chars().count()
3939
}
4040
}
@@ -104,26 +104,26 @@ impl Visitor for WikilinkVisitor {
104104
.expect("Otherwise the regex wouldn't match")
105105
.as_str(),
106106
);
107+
let capture_start_byte = captures
108+
.get(1)
109+
.expect("The regex has 2 capture groups")
110+
.start();
111+
let text_without_frontmatter = remove_frontmatter_from_source(source, node);
112+
let sourcepos_start_offset_bytes = SourceOffset::from_location(
113+
text_without_frontmatter,
114+
sourcepos.start.line,
115+
sourcepos.start.column,
116+
)
117+
.offset();
118+
let span = SourceSpan::new(
119+
(sourcepos_start_offset_bytes + capture_start_byte).into(),
120+
alias.char_len(),
121+
);
122+
let span_repaired = repair_span_due_to_frontmatter(span, node);
107123
self.wikilinks.push(
108124
Wikilink::builder()
109125
.alias(alias.clone())
110-
.span(repair_span_due_to_frontmatter(
111-
SourceSpan::new(
112-
(SourceOffset::from_location(
113-
remove_frontmatter_from_source(source, node),
114-
sourcepos.start.line,
115-
sourcepos.start.column,
116-
)
117-
.offset()
118-
+ captures
119-
.get(1)
120-
.expect("The regex has 2 capture groups")
121-
.start())
122-
.into(),
123-
alias.len(),
124-
),
125-
node,
126-
))
126+
.span(span_repaired)
127127
.build(),
128128
);
129129
}

src/rules/unlinked_text.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,20 @@ impl Visitor for UnlinkedTextVisitor {
181181
continue;
182182
}
183183
let alias = Alias::new(&patterns[found.pattern().as_usize()]);
184-
let span = repair_span_due_to_frontmatter(
185-
SourceSpan::new(
186-
(SourceOffset::from_location(
187-
remove_frontmatter_from_source(source, node),
188-
sourcepos.start.line,
189-
sourcepos.start.column,
190-
)
191-
.offset()
192-
+ found.start())
193-
.into(),
194-
found.end() - found.start(),
195-
),
196-
node,
197-
);
184+
if "lorem" == alias.to_string() {
185+
println!("Found lorem");
186+
}
187+
let text_without_frontmatter = remove_frontmatter_from_source(source, node);
188+
let sourcepos_start_offset_bytes = SourceOffset::from_location(
189+
text_without_frontmatter,
190+
sourcepos.start.line,
191+
sourcepos.start.column,
192+
)
193+
.offset();
194+
let byte_length = found.end() - found.start();
195+
let offset_bytes = sourcepos_start_offset_bytes + found.start();
196+
let span = SourceSpan::new(offset_bytes.into(), byte_length);
197+
let span_repaired = repair_span_due_to_frontmatter(span, node);
198198

199199
// Dont match inside wikilinks
200200
if let Some(parent) = parent {
@@ -204,7 +204,7 @@ impl Visitor for UnlinkedTextVisitor {
204204
}
205205
}
206206

207-
self.new_unlinked_texts.push((alias, span));
207+
self.new_unlinked_texts.push((alias, span_repaired));
208208
}
209209
}
210210
Ok(())

0 commit comments

Comments
 (0)