Skip to content

Commit

Permalink
custom paredit (#1802)
Browse files Browse the repository at this point in the history
* Add git submodule paredit.js

* Configure linters to ignore the vendor dir

* Install the local paredit.js

* Implement the `emacs-mcx.paredit.parentheses` config

* Add tests

* Apply formatter to package.json

* Fix the CI to checkout submodules

* Fix lint staged config

* Update README.md

* Update .vscodeignore

* Fix lint-staged.config.mjs

* Fix lint-staged config

* Fix lint-staged config

* Fix lint-staged config

* Fix .vscodeignore

* Update .vscodeignore

* Update
  • Loading branch information
whitphx authored Jan 11, 2024
1 parent 0f6ee06 commit 165302b
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 10 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
src/vs/
vendor/
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Install Node.js
uses: actions/setup-node@v4
with:
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vendor/paredit.js"]
path = vendor/paredit.js
url = https://github.com/whitphx/paredit.js.git
4 changes: 0 additions & 4 deletions .lintstagedrc.json

This file was deleted.

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist
out
src/vs/
keybindings.json
vendor/
6 changes: 5 additions & 1 deletion .vscodeignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ out/
src/
keybinding-generator/
scripts/
vendor/
.gitignore
.eslintignore
.prettierignore
Expand All @@ -26,5 +27,8 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.husky
.lintstagedrc.json
lint-staged.config.js
.git-blame-ignore-revs
.eslintcache
.gitmodules
.nvmrc
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ Set `false` when `M-<digit>` conflicts with some other necessary commands. See h

When true, line-move moves point by visual lines (same as an Emacs variable line-move-visual).

### `emacs-mcx.paredit.parentheses`

Key-value pairs of parentheses like the following example to be used in the ParEdit commands.

```json
{
"[": "]",
"(": ")",
"{": "}"
}
```

### `emacs-mcx.debug.*`

Configurations for debugging.
Expand Down
22 changes: 22 additions & 0 deletions lint-staged.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-env node */
/* eslint-disable @typescript-eslint/no-var-requires */

const path = require("path");
const micromatch = require("micromatch");

module.exports = {
"*.{js,ts,mjs,mts}": (files) => {
const match = micromatch.not(files, path.join(__dirname, "./vendor/*"));
if (match.length === 0) {
return [];
}
return `eslint --cache --fix ${match.join(" ")}`;
},
"*.{js,ts,mjs,mts,md,json,yml}": (files) => {
const match = micromatch.not(files, path.join(__dirname, "./vendor/*"));
if (match.length === 0) {
return [];
}
return `prettier --write ${match.join(" ")}`;
},
};
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@
"default": true,
"description": "When true, line-move moves point by visual lines (same as an Emacs variable line-move-visual)."
},
"emacs-mcx.paredit.parentheses": {
"type": "object",
"patternProperties": {
"^\\S$": {
"type": "string",
"pattern": "^\\S$"
}
},
"default": {
"[": "]",
"(": ")",
"{": "}"
}
},
"emacs-mcx.debug.silent": {
"type": "boolean",
"description": "If true, all logs are suppressed.",
Expand Down Expand Up @@ -6695,7 +6709,7 @@
"webpack-cli": "^5.1.4"
},
"dependencies": {
"paredit.js": "^0.4.0",
"paredit.js": "./vendor/paredit.js",
"winston": "^3.11.0",
"winston-console-for-electron": "^0.0.7"
}
Expand Down
10 changes: 9 additions & 1 deletion src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import { Logger } from "../logger";
import * as vscode from "vscode";
import { IConfiguration, IDebugConfiguration } from "./iconfiguration";
import { IConfiguration, IDebugConfiguration, IPareditConfiguration } from "./iconfiguration";
import * as paredit from "paredit.js";

export class Configuration implements IConfiguration, vscode.Disposable {
/**
Expand Down Expand Up @@ -40,6 +41,10 @@ export class Configuration implements IConfiguration, vscode.Disposable {

public lineMoveVisual = true;

public paredit: IPareditConfiguration = {
parentheses: { "[": "]", "(": ")", "{": "}" },
};

public debug: IDebugConfiguration = {
silent: false,
loggingLevelForAlert: "error",
Expand Down Expand Up @@ -77,6 +82,9 @@ export class Configuration implements IConfiguration, vscode.Disposable {
}

Logger.configChanged(this);

// Update configs in the third-party libraries.
paredit.reader.setParentheses(this.paredit.parentheses);
}

private static unproxify(obj: { [key: string]: unknown }) {
Expand Down
9 changes: 9 additions & 0 deletions src/configuration/iconfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export interface IPareditConfiguration {
parentheses: { [key: string]: string };
}

export interface IDebugConfiguration {
/**
* Boolean indicating whether all logs should be suppressed
Expand Down Expand Up @@ -39,6 +43,11 @@ export interface IConfiguration {
*/
lineMoveVisual: boolean;

/**
* Paredit configuration
*/
paredit: IPareditConfiguration;

/**
* Extension debugging settings
*/
Expand Down
48 changes: 48 additions & 0 deletions src/test/suite/commands/paredit.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import assert from "assert";
import * as vscode from "vscode";
import * as sinon from "sinon";
import { Selection, TextEditor } from "vscode";
import { EmacsEmulator } from "../../../emulator";
import { KillRing } from "../../../kill-yank/kill-ring";
Expand All @@ -11,6 +13,7 @@ import {
clearTextEditor,
assertSelectionsEqual,
} from "../utils";
import { Configuration } from "../../../configuration/configuration";

suite("paredit commands", () => {
let activeTextEditor: TextEditor;
Expand Down Expand Up @@ -64,6 +67,51 @@ suite("paredit commands", () => {
});
});

suite("Parentheses config", () => {
let activeTextEditor: TextEditor;
let emulator: EmacsEmulator;
let getConfigurationStub: sinon.SinonStub;

setup(async () => {
const initialText = "<<>>";

activeTextEditor = await setupWorkspace(initialText);
emulator = new EmacsEmulator(activeTextEditor);

getConfigurationStub = sinon.stub(vscode.workspace, "getConfiguration");
});
teardown(async () => {
getConfigurationStub.restore();
Configuration.reload();
await cleanUpWorkspace();
});

function mockPareditConfig(parentheses: Record<string, string>) {
getConfigurationStub.returns({
paredit: {
parentheses,
},
});
Configuration.reload();
}

test("forwardSexp", async () => {
mockPareditConfig({ "(": ")" });
setEmptyCursors(activeTextEditor, [0, 1]);
await emulator.runCommand("paredit.forwardSexp");
assertCursorsEqual(activeTextEditor, [0, 4]);
await emulator.runCommand("paredit.forwardSexp");
assertCursorsEqual(activeTextEditor, [0, 4]);

mockPareditConfig({ "<": ">" });
setEmptyCursors(activeTextEditor, [0, 1]);
await emulator.runCommand("paredit.forwardSexp");
assertCursorsEqual(activeTextEditor, [0, 3]);
await emulator.runCommand("paredit.forwardSexp");
assertCursorsEqual(activeTextEditor, [0, 3]);
});
});

suite("paredit.kill-sexp", () => {
const initialText = `(
(
Expand Down
1 change: 1 addition & 0 deletions vendor/paredit.js
Submodule paredit.js added at a6f9bd
4 changes: 1 addition & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3104,10 +3104,8 @@ pako@~1.0.2:
resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.11.tgz#6c9599d340d54dfd3946380252a35705a6b992bf"
integrity sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==

paredit.js@^0.4.0:
paredit.js@./vendor/paredit.js:
version "0.4.0"
resolved "https://registry.yarnpkg.com/paredit.js/-/paredit.js-0.4.0.tgz#a4bb7eeef8d4e5601d2dcc3f7929e5fea1e4f240"
integrity sha512-Zw1yo0DzKOjoI/VNoHINrJ9zis2it188XJRdjt2yAusSqr4Ch2Vq0hbzpqKEa+BMtWBHCVlfzpgXpM5b1TPVbA==
dependencies:
ace.improved ">=0.1.4"

Expand Down

0 comments on commit 165302b

Please sign in to comment.