Skip to content

Commit 7fa767d

Browse files
author
Joe
committed
initial commit 🎉
0 parents  commit 7fa767d

File tree

9 files changed

+1312
-0
lines changed

9 files changed

+1312
-0
lines changed

.github/workflows/ci.yml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: main
2+
on: [push]
3+
jobs:
4+
build-and-test:
5+
runs-on: ubuntu-latest
6+
name: Build
7+
steps:
8+
- uses: actions/checkout@v1
9+
10+
- uses: actions/setup-node@v2
11+
with:
12+
node-version: "16.4"
13+
14+
- run: yarn
15+
- run: yarn build

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/node_modules
2+
package-lock.json
3+
/dist

.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
index.ts
2+
/node_modules
3+
.tern-*
4+
tsconfig.json

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (C) 2018-2021 by Joe Previte <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Vim keybindings for CM6
2+
3+
## Installation
4+
5+
`npm i @replit/codemirror-vim`
6+
7+
## Usage
8+
9+
```js
10+
import { basicSetup, EditorState } from '@codemirror/basic-setup';
11+
import { EditorView } from '@codemirror/view';
12+
import { vim } from "@replit/codemirror-vim"
13+
14+
new EditorView({
15+
state: EditorState.create({
16+
doc: "",
17+
extensions: [
18+
// make sure vim is included before other keymaps
19+
vim(),
20+
// include the default keymap and all other keymaps you want to use in insert mode
21+
basicSetup,
22+
]
23+
}),
24+
parent: document.querySelector('#editor'),
25+
})
26+
```

index.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Extension } from "@codemirror/state";
2+
import { EditorView, ViewUpdate } from "@codemirror/view";
3+
import { Compartment, EditorState } from "@codemirror/state";
4+
import { lineNumbers } from "@codemirror/gutter";
5+
6+
let gutter = new Compartment();
7+
8+
function relativeLineNumbers(lineNo: number, state: EditorState) {
9+
if (lineNo > state.doc.lines) {
10+
return "0";
11+
}
12+
const cursorLine = state.doc.lineAt(
13+
state.selection.asSingle().ranges[0].to
14+
).number;
15+
if (lineNo === cursorLine) {
16+
return "0";
17+
} else {
18+
return Math.abs(cursorLine - lineNo).toString();
19+
}
20+
}
21+
// This shows the numbers in the gutter
22+
const showLineNumbers = gutter.of(
23+
lineNumbers({ formatNumber: relativeLineNumbers })
24+
);
25+
26+
// This ensures the numbers update
27+
// when selection (cursorActivity) happens
28+
const lineNumbersUpdateListener = EditorView.updateListener.of(
29+
(viewUpdate: ViewUpdate) => {
30+
if (viewUpdate.selectionSet) {
31+
viewUpdate.view.dispatch({
32+
effects: gutter.reconfigure(
33+
lineNumbers({ formatNumber: relativeLineNumbers })
34+
),
35+
});
36+
}
37+
}
38+
);
39+
40+
export function lineNumbersRelative(): Extension {
41+
return [showLineNumbers, lineNumbersUpdateListener];
42+
}

package.json

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "codemirror-line-numbers-relative",
3+
"version": "0.19.0-beta.1",
4+
"description": "Relative line numbers for CodeMirror 6",
5+
"scripts": {
6+
"build": "cm-buildhelper index.ts"
7+
},
8+
"keywords": [
9+
"editor",
10+
"code",
11+
"relative line numbers"
12+
],
13+
"type": "module",
14+
"main": "dist/index.cjs",
15+
"exports": {
16+
"import": "./dist/index.js",
17+
"require": "./dist/index.cjs"
18+
},
19+
"types": "dist/index.d.ts",
20+
"module": "dist/index.js",
21+
"sideEffects": false,
22+
"license": "MIT",
23+
"peerDependencies": {
24+
"@codemirror/state": "^0.19.3",
25+
"@codemirror/view": "^0.19.3",
26+
"@codemirror/gutter": "^0.19.3"
27+
},
28+
"devDependencies": {
29+
"@codemirror/buildhelper": "^0.1.11",
30+
"@codemirror/lang-javascript": "^0.19.3"
31+
},
32+
"repository": {
33+
"type": "git",
34+
"url": "https://github.com/replit/codemirror-vim.git"
35+
}
36+
}

tsconfig.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": false,
4+
"checkJs": false,
5+
"lib": ["es6", "dom", "scripthost"],
6+
"stripInternal": true,
7+
"typeRoots": ["./node_modules/@types"],
8+
"noUnusedLocals": true,
9+
"strict": true,
10+
"target": "es6",
11+
"module": "es2020",
12+
"newLine": "lf",
13+
"moduleResolution": "node",
14+
},
15+
"include": [
16+
"index.ts"
17+
]
18+
}

0 commit comments

Comments
 (0)