Skip to content

Commit

Permalink
add spell check scope config
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarlevin committed Jul 14, 2024
1 parent 3d7048a commit 111dc1e
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Configuration defaults for cSpell spell checker to enable spell checking in pretext documents, and to ignore words inside tags and between math-mode tags (`<m>`, `<me>`, etc.).
- Spell check scopes are configurable: you can specify whether to check or ignore spelling inside comments, math, latex-image, code, etc.
- Support for CLI 2.5+ schema locations.

## [0.19.0] - 2024-07-06
Expand Down
88 changes: 77 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,6 @@
"xml.validation.xInclude.enabled": true,
"cSpell.enableFiletypes": [
"pretext"
],
"cSpell.languageSettings": [
{
"languageId": "pretext",
"ignoreRegExpList": [
"<.*?>",
"<m>.*?</m>",
"<(me|men|md|mdn)>(.|\n|\r|\n\r)*?</(me|men|md|mdn)>",
"<latex-image>(.|\n|\r|\n\r)*?</latex-image>"
]
}
]
},
"menus": {},
Expand All @@ -224,6 +213,7 @@
"markdownDescription": "Select which method to use when viewing the output of a pretext project. Leaving this at the default will ask for available methods each time. Your choice will likely depend on the size of your project and whether you are building output other than the 'web' target."
},
"pretext-tools.formatter.blankLines": {
"order": 4,
"type": "string",
"default": "some",
"enum": [
Expand All @@ -239,16 +229,90 @@
"markdownDescription": "Select whether few, some, or many blank lines appear when the document is formatted."
},
"pretext-tools.formatter.breakSentences": {
"order": 4,
"type": "boolean",
"default": true,
"markdownDescription": "Whether to add a line break after each period in a paragraph."
},
"pretext-tools.spellCheck.checkErrorsInsideScope": {
"order": 3,
"type": "object",
"properties": {
"comments": {
"type": "string",
"default": "Check",
"enum": [
"Check",
"Ignore"
],
"markdownDescription": "Check or Ignore spelling in comments."
},
"inlineMath": {
"type": "string",
"default": "Ignore",
"enum": [
"Check",
"Ignore"
],
"markdownDescription": "Check or Ignore spelling for inline math (`<m>` tags)."
},
"displayMath": {
"type": "string",
"default": "Ignore",
"enum": [
"Check",
"Ignore"
],
"markdownDescription": "Check or Ignore spelling in display math (`<me>`, `<md>`, etc)."
},
"inlineCode": {
"type": "string",
"default": "Ignore",
"enum": [
"Check",
"Ignore"
],
"markdownDescription": "Check or Ignore spelling in inline code (`<c>` tags)."
},
"blockCode": {
"type": "string",
"default": "Ignore",
"enum": [
"Check",
"Ignore"
],
"markdownDescription": "Check or Ignore spelling in program elements."
},
"latexImage": {
"type": "string",
"default": "Ignore",
"enum": [
"Check",
"Ignore"
],
"markdownDescription": "Check or Ignore spelling in latex-image."
},
"tags": {
"type": "string",
"default": "Ignore",
"enum": [
"Check",
"Ignore"
],
"markdownDescription": "Check or Ignore spelling in tags and attributes."
}
},
"markdownDescription": "Check or ignore spelling inside specific scopes. You must have the Code Spell Checker extension installed for this to work and need to ensure that pretext files are set to be checked in the settings of that extension.",
"additionalProperties": false
},
"pretext-tools.installPretext": {
"order": 10,
"type": "boolean",
"default": true,
"markdownDescription": "Whether pretext-tools should try to use pip to install PreTeXt if it cannot find it."
},
"pretext-tools.schema.Version": {
"order": 1,
"type": "string",
"default": "Stable",
"enum": [
Expand All @@ -264,11 +328,13 @@
"markdownDescription": "Select the version of the PreTeXt schema to validate against. If you are using newer features, you might try to experimental schema, which isn't as stable, but will give you fewer warnings."
},
"pretext-tools.schema.customPath": {
"order": 2,
"type": "string",
"default": "",
"markdownDescription": "Provide a path to your custom schema file. This will override the schema version setting."
},
"pretext-tools.pythonPath": {
"order": 20,
"type": "string",
"default": "",
"markdownDescription": "To use a non-standard python installation or virtual environment, set path to python here. If blank we will use `python -m` or `python3 -m`. **Requires restart of VS Code**"
Expand Down
21 changes: 19 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,34 @@ export async function activate(context: vscode.ExtensionContext) {

vscode.workspace.onDidChangeConfiguration((event) => {
let affected = event.affectsConfiguration("pretext-tools");
if (affected) {
console.log("PreTeXt Tools configuration changed");
if (event.affectsConfiguration("pretext-tools.schema")) {
console.log("PreTeXt Tools schema configuration changed");
// Set schema for pretext files:
try {
utils.setSchema();
} catch {
console.log("Error setting schema");
}
}
if (event.affectsConfiguration("pretext-tools.spellCheck")) {
console.log("PreTeXt Tools spell check configuration changed");
// Set spell check options:
try {
utils.setSpellCheckConfig();
} catch {
console.log("Error setting spell check");
}
}
});

// Set spell check options:
try {
utils.setSpellCheckConfig();
}
catch {
console.log("Error setting spell check");
}

// Set schema for pretext files:
try {
utils.setSchema();
Expand Down
45 changes: 45 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { homedir } from "os";
import * as path from "path";
import * as vscode from "vscode";
import * as fs from "fs";
import { SpellCheckScope } from "./types";

export {
getDir,
installPretext,
ptxExec,
getTargets,
setSchema,
setSpellCheckConfig,
updateStatusBarItem,
setupTerminal,
};
Expand Down Expand Up @@ -295,6 +297,49 @@ function getTargets() {
}
}

function setSpellCheckConfig() {
const cSpellConfig = vscode.workspace.getConfiguration("cSpell");
// Now update which scopes should be checked or ignored for spell checking.
const spellCheckScopes: SpellCheckScope | undefined = vscode.workspace.getConfiguration("pretext-tools").get("spellCheck.checkErrorsInsideScope");
console.log("Current value of spellCheck.checkErrorsInsideScope is", spellCheckScopes);
let ignorePatterns: string[] = [];
if (spellCheckScopes) {
if (spellCheckScopes.comments === "Ignore") {
ignorePatterns.push("<!--.*?-->");
}
if (spellCheckScopes.inlineMath === "Ignore") {
ignorePatterns.push("<m>.*?</m>");
}
if (spellCheckScopes.displayMath === "Ignore") {
ignorePatterns.push("<(me|men|md|mdn)>(.|\n|\r|\n\r)*?</(me|men|md|mdn)>");
}
if (spellCheckScopes.inlineCode === "Ignore") {
ignorePatterns.push("<c>.*?</c>");
}
if (spellCheckScopes.blockCode === "Ignore") {
ignorePatterns.push("<(program|sage|pre)>(.|\n|\r|\n\r)*?</(program|sage|pre)>");
}
if (spellCheckScopes.latexImage === "Ignore") {
ignorePatterns.push("<latex-image>(.|\n|\r|\n\r)*?</latex-image>");
}
if (spellCheckScopes.tags === "Ignore") {
ignorePatterns.push("<[^!].*>");
}
}
// Get current languageSettings for cSpell and update those for pretext
let languageSettings: any = cSpellConfig.get("languageSettings");
for (let dicts of languageSettings) {
if (dicts["languageId"] === "pretext") {
console.log("Current value of languageSettings for Pretext is", dicts);
dicts["ignoreRegExpList"] = ignorePatterns;
break;
}
}
console.log("Updated languageSettings for Pretext to", languageSettings);
cSpellConfig.update("languageSettings", languageSettings);
}


function setSchema() {
let schemaPath: string | undefined = vscode.workspace
.getConfiguration("pretext-tools")
Expand Down

0 comments on commit 111dc1e

Please sign in to comment.