Skip to content

Commit

Permalink
Merge pull request #449 from Automattic/default-config
Browse files Browse the repository at this point in the history
 feat(harper.js): added ability to explicitly set config to the default
  • Loading branch information
elijah-potter authored Jan 22, 2025
2 parents 280aaea + 41aeba7 commit bae254e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
19 changes: 19 additions & 0 deletions harper-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,25 @@ impl Lint {
}
}

#[wasm_bindgen]
pub fn get_default_lint_config_as_json() -> String {
let mut config = LintGroupConfig::default();
config.fill_default_values();

serde_json::to_string(&config).unwrap()
}

#[wasm_bindgen]
pub fn get_default_lint_config() -> JsValue {
let mut config = LintGroupConfig::default();
config.fill_default_values();

// Important for downstream JSON serialization
let serializer = serde_wasm_bindgen::Serializer::json_compatible();

config.serialize(&serializer).unwrap()
}

/// A struct that represents two character indices in a string: a start and an end.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[wasm_bindgen]
Expand Down
10 changes: 10 additions & 0 deletions packages/harper.js/src/Linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ for (const [linterName, Linter] of Object.entries(linters)) {
expect(value).not.toHaveLength(0);
}
});

test(`${linterName} default lint config has no null values`, async () => {
const linter = new Linter();

const lintConfig = await linter.getDefaultLintConfig();

for (const value of Object.values(lintConfig)) {
expect(value).not.toBeNull();
}
});
}

test('Linters have the same config format', async () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/harper.js/src/Linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export default interface Linter {
/** Get the linter's current configuration. */
getLintConfig(): Promise<LintConfig>;

/** Get the default (unset) linter configuration as JSON.
* This method does not effect the caller's lint configuration, nor does it return the current one. */
getDefaultLintConfigAsJSON(): Promise<string>;

/** Get the default (unset) linter configuration.
* This method does not effect the caller's lint configuration, nor does it return the current one. */
getDefaultLintConfig(): Promise<LintConfig>;

/** Set the linter's current configuration. */
setLintConfig(config: LintConfig): Promise<void>;

Expand Down
12 changes: 12 additions & 0 deletions packages/harper.js/src/LocalLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ export default class LocalLinter implements Linter {
return this.inner!.get_lint_config_as_object();
}

async getDefaultLintConfigAsJSON(): Promise<string> {
const wasm = await loadWasm();

return wasm.get_default_lint_config_as_json();
}

async getDefaultLintConfig(): Promise<LintConfig> {
const wasm = await loadWasm();

return wasm.get_default_lint_config();
}

async setLintConfig(config: LintConfig): Promise<void> {
await this.initialize();

Expand Down
8 changes: 8 additions & 0 deletions packages/harper.js/src/WorkerLinter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ export default class WorkerLinter implements Linter {
return JSON.parse(await this.getLintDescriptionsAsJSON()) as Record<string, string>;
}

getDefaultLintConfigAsJSON(): Promise<string> {
return this.rpc('getDefaultLintConfigAsJSON', []);
}

async getDefaultLintConfig(): Promise<LintConfig> {
return JSON.parse(await this.getDefaultLintConfigAsJSON()) as LintConfig;
}

/** Run a procedure on the remote worker. */
private async rpc(procName: string, args: any[]): Promise<any> {
const promise = new Promise((resolve, reject) => {
Expand Down
34 changes: 34 additions & 0 deletions packages/web/src/lib/DefaultNeovimConfig.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script>
import { LocalLinter } from 'harper.js';
import { Button } from 'flowbite-svelte';
let linter = new LocalLinter();
let head = `lspconfig.harper_ls.setup {
settings = {
["harper-ls"] = {
linters = {
`;
let tail = ` }
}
},
}`;
async function generateConfig() {
let default_config = await linter.getDefaultLintConfig();
let rows = Object.entries(default_config)
.map(([key, value]) => `\t\t\t${key} = ${value},`)
.reduce((prev, cur) => prev + '\n' + cur);
return head + rows + tail;
}
async function copyConfig() {
let defaultConfig = await generateConfig();
navigator.clipboard.writeText(defaultConfig);
}
</script>

<Button onclick={copyConfig}>Copy Default Config to Clipboard</Button>
6 changes: 6 additions & 0 deletions packages/web/src/routes/docs/integrations/neovim/+page.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ lspconfig.harper_ls.setup {
}
```

<script>
import DefaultNeovimConfig from "$lib/DefaultNeovimConfig.svelte"
</script>

<DefaultNeovimConfig/>

By default, `harper-ls` will mark all diagnostics with HINT.
If you want to configure this, refer below:

Expand Down

0 comments on commit bae254e

Please sign in to comment.