feat(obsidian): add method to ignore all errors in file (#2105)

This commit is contained in:
Elijah Potter 2025-10-24 13:31:52 -06:00 committed by GitHub
parent efde39eb79
commit 1945a524e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 61 additions and 8 deletions

View file

@ -1,10 +1,9 @@
import type { Extension, StateField } from '@codemirror/state';
import type { LintConfig, Linter, Suggestion } from 'harper.js';
import type { Lint, LintConfig, Linter, Suggestion } from 'harper.js';
import { binaryInlined, type Dialect, LocalLinter, SuggestionKind, WorkerLinter } from 'harper.js';
import { minimatch } from 'minimatch';
import type { MarkdownFileInfo, MarkdownView, Workspace } from 'obsidian';
import { linter } from './lint';
import { charIndexToCodeUnit } from './textUtils';
export type Settings = {
ignoredLints?: string;
@ -196,8 +195,7 @@ export default class State {
return node.content;
},
ignore: async () => {
await this.harper.ignoreLint(text, lint);
await this.reinitialize();
await this.ignoreLints(text, [lint]);
},
actions,
};
@ -209,6 +207,15 @@ export default class State {
);
}
/** Use this method instead of interacting with the linter directly. */
public async ignoreLints(text: string, lints: Lint[]) {
for (const lint of lints) {
await this.harper.ignoreLint(text, lint);
}
await this.reinitialize();
}
public async reinitialize() {
const settings = await this.getSettings();
await this.initializeFromSettings(settings);
@ -332,6 +339,12 @@ export default class State {
this.enableEditorLinter();
}
}
/** Get a reference to the current linter.
* It's best not to hold on to this type and to instead use this function again if another reference is needed. */
public getLinter(): Linter {
return this.harper;
}
}
function suggestionToLabel(sug: Suggestion) {

View file

@ -84,8 +84,16 @@ export default class HarperPlugin extends Plugin {
.setTitle(`${this.state.hasEditorLinter() ? 'Disable' : 'Enable'} automatic checking`)
.setIcon('documents')
.onClick(() => {
this.state.toggleAutoLint();
this.updateStatusBar();
this.toggleAutoLint();
}),
);
menu.addItem((item) =>
item
.setTitle('Ignore all errors in file')
.setIcon('eraser')
.onClick(() => {
this.doIgnoreAllFlow();
}),
);
@ -95,15 +103,47 @@ export default class HarperPlugin extends Plugin {
statusBarItem.appendChild(button);
}
/** Preferred over directly calling `this.state.toggleAutoLint()` */
private toggleAutoLint() {
this.state.toggleAutoLint();
this.updateStatusBar();
}
private setupCommands() {
this.addCommand({
id: 'harper-toggle-auto-lint',
name: 'Toggle automatic grammar checking',
callback: () => {
this.state.toggleAutoLint();
this.updateStatusBar();
this.toggleAutoLint();
},
});
this.addCommand({
id: 'harper-ignore-all-in-buffer',
name: 'Ignore all errors in the open file',
callback: async () => {
await this.doIgnoreAllFlow();
},
});
}
/** Trigger the flow for ignoring all files in a document, including a confirmation modal. */
public async doIgnoreAllFlow() {
const file = this.app.workspace.getActiveFile();
if (file != null) {
const text = await this.app.vault.read(file);
const lints = await this.state.getLinter().lint(text);
const confirmation = confirm(
`Are you sure you want to ignore ${lints.length} errors from Harper?`,
);
if (confirmation) {
await this.state.ignoreLints(text, lints);
}
} else {
new Notice('No file currently open.');
}
}
public updateStatusBar(dialect?: Dialect) {