Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(obsidian): problems when converting from Rust char indexes (#2104)

Elijah Potteraeb303b4fa20
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index 66e47d900..f257c542b 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -280,7 +280,9 @@ impl Linter {
.into_iter()
.map(|l| {
let problem_text = l.span.get_content_string(&source);
- Lint::new(l, problem_text, language)
+ let span = Into::<Span>::into(l.span).to_js_indices(source.as_slice());
+
+ Lint::new(l, span, problem_text, language)
})
.collect(),
})
@@ -310,7 +312,8 @@ impl Linter {
.into_iter()
.map(|l| {
let problem_text = l.span.get_content_string(&source);
- Lint::new(l, problem_text, language)
+ let span = Into::<Span>::into(l.span).to_js_indices(source.as_slice());
+ Lint::new(l, span, problem_text, language)
})
.collect()
}
@@ -471,6 +474,8 @@ impl Suggestion {
#[wasm_bindgen]
pub struct Lint {
inner: harper_core::linting::Lint,
+ /// Indexed in a proverbial JS string
+ span: Span,
/// The problematic text that produced this lint.
problem_text: String,
language: Language,
@@ -480,11 +485,13 @@ pub struct Lint {
impl Lint {
pub(crate) fn new(
inner: harper_core::linting::Lint,
+ span: Span,
problem_text: String,
language: Language,
) -> Self {
Self {
inner,
+ span,
problem_text,
language,
}
@@ -521,7 +528,7 @@ impl Lint {
/// Get the location of the problematic text.
pub fn span(&self) -> Span {
- self.inner.span.into()
+ self.span
}
/// Get a description of the error.
@@ -535,6 +542,13 @@ impl Lint {
}
}
+/// Convert Harper's character index into a UTF-16 code unit index understood by JS.
+fn char_idx_to_js_str_idx(char_idx: usize, char_str: &[char]) -> usize {
+ char_str.iter().take(char_idx).fold(0usize, |acc, ch| {
+ acc + if (*ch as u32) <= 0xFFFF { 1 } else { 2 }
+ })
+}
+
#[wasm_bindgen]
pub fn get_default_lint_config_as_json() -> String {
let config =
@@ -577,6 +591,15 @@ impl Span {
}
}
+impl Span {
+ pub fn to_js_indices(&self, source: &[char]) -> Self {
+ Self::new(
+ char_idx_to_js_str_idx(self.start, source),
+ char_idx_to_js_str_idx(self.end, source),
+ )
+ }
+}
+
impl From<Span> for harper_core::Span<char> {
fn from(value: Span) -> Self {
harper_core::Span::new(value.start, value.end)
diff --git a/packages/harper.js/src/Linter.test.ts b/packages/harper.js/src/Linter.test.ts
index b217c9fa9..977ab6acf 100644
--- a/packages/harper.js/src/Linter.test.ts
+++ b/packages/harper.js/src/Linter.test.ts
@@ -333,6 +333,20 @@ for (const [linterName, Linter] of Object.entries(linters)) {
const newLinter = new Linter({ binary });
await newLinter.importStatsFile(stats);
});
+
+ test(`${linterName} emits the correct span indices`, async () => {
+ const text = '✉️👋👍✉️🚀✉️🌴 This is to show the offset issue sdssda is it there?';
+
+ const linter = new LocalLinter({ binary });
+ const lints = await linter.lint(text);
+
+ const span = lints[0].span();
+
+ expect(span.start).toBe(48);
+ expect(span.end).toBe(54);
+
+ expect(text.slice(span.start, span.end)).toBe('sdssda');
+ });
}
test('Linters have the same config format', async () => {
diff --git a/packages/obsidian-plugin/src/State.ts b/packages/obsidian-plugin/src/State.ts
index a5e31bc71..9849b2192 100644
--- a/packages/obsidian-plugin/src/State.ts
+++ b/packages/obsidian-plugin/src/State.ts
@@ -1,10 +1,10 @@
import type { Extension, StateField } from '@codemirror/state';
import type { LintConfig, Linter, Suggestion } from 'harper.js';
import { binaryInlined, type Dialect, LocalLinter, SuggestionKind, WorkerLinter } from 'harper.js';
-import { toArray } from 'lodash-es';
import { minimatch } from 'minimatch';
import type { MarkdownFileInfo, MarkdownView, Workspace } from 'obsidian';
import { linter } from './lint';
+import { charIndexToCodeUnit } from './textUtils';
export type Settings = {
ignoredLints?: string;
@@ -128,16 +128,13 @@ export default class State {
}
const text = view.state.doc.sliceString(-1);
- const chars = toArray(text);
+ const chars = Array.from(text);
const lints = await this.harper.lint(text);
return lints.map((lint) => {
const span = lint.span();
- span.start = charIndexToCodePointIndex(span.start, chars);
- span.end = charIndexToCodePointIndex(span.end, chars);
-
const actions = lint.suggestions().map((sug) => {
return {
name:
@@ -334,26 +331,11 @@ export default class State {
}
}
-/** Harper returns positions based on char indexes,
- * but Obsidian identifies locations in documents based on Unicode code points.
- * This converts between from the former to the latter.*/
-function charIndexToCodePointIndex(index: number, sourceChars: string[]): number {
- let traversed = 0;
-
- for (let i = 0; i < index; i++) {
- const delta = sourceChars[i].length;
-
- traversed += delta;
- }
-
- return traversed;
-}
-
function suggestionToLabel(sug: Suggestion) {
if (sug.kind() === SuggestionKind.Remove) {
return 'Remove';
} else if (sug.kind() === SuggestionKind.Replace) {
- return `“Replace with ${sug.get_replacement_text()}”`;
+ return `Replace with “${sug.get_replacement_text()}”`;
} else if (sug.kind() === SuggestionKind.InsertAfter) {
return `Insert “${sug.get_replacement_text()}” after this.`;
}
diff --git a/packages/obsidian-plugin/src/textUtils.test.ts b/packages/obsidian-plugin/src/textUtils.test.ts
index fdd527db2..15c03b9af 100644
--- a/packages/obsidian-plugin/src/textUtils.test.ts
+++ b/packages/obsidian-plugin/src/textUtils.test.ts
@@ -1,4 +1,5 @@
-import { expect, test } from 'vitest';
+import { binary, LocalLinter } from 'harper.js';
+import { describe, expect, test } from 'vitest';
import { linesToString, stringToLines } from './textUtils';
test('Dictionary values are reversible', () => {