-
Notifications
You must be signed in to change notification settings - Fork 5
/
extension.ts
221 lines (191 loc) · 6.36 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
import * as vscode from 'vscode';
import * as fs from 'fs';
import * as _ from 'lodash';
import { attach } from 'neovim';
import { NeovimClient } from 'neovim/lib/api/client';
import { TaskQueue } from 'aurelia-task-queue';
import { Position } from './src/common/motion/position';
import { Globals } from './src/globals';
import { Configuration } from './src/configuration/configuration';
import { spawn } from 'child_process';
import { NvUtil } from './srcNV/nvUtil';
import { RpcRequest } from './srcNV/rpcHandlers';
import { TextEditor } from './src/textEditor';
import { Screen, IgnoredKeys } from './srcNV/screen';
import { VimSettings } from './srcNV/vimSettings';
import { VscHandlers } from './srcNV/vscHandlers';
interface VSCodeKeybinding {
key: string;
command: string;
when: string;
vimKey: string;
}
const packagejson: {
contributes: {
keybindings: VSCodeKeybinding[];
};
} = require('../package.json'); // out/../package.json
export namespace Vim {
export let nv: NeovimClient;
export let channelId: number;
export let mode: { mode: string; blocking: boolean } = { mode: 'n', blocking: false };
export let screen: Screen;
export let prevState: { bufferTick: number } = {
bufferTick: -1,
};
export let numVimChangesToApply = 0;
export let taskQueue = new TaskQueue();
// We're connecting to an already existing terminal instance, so externalized ui won't work.
export let DEBUG: boolean;
}
export async function activate(context: vscode.ExtensionContext) {
vscode.workspace.onDidCloseTextDocument(async event => {
const deleted_file = event.fileName;
let buf_id = await nvim.call('bufnr', [`^${deleted_file}$`]);
if (buf_id === -1) {
return;
}
// await nvim.command(`noautocmd ${buf_id}bw!`);
});
vscode.window.onDidChangeActiveTextEditor(VscHandlers.handleActiveTextEditorChange, this);
vscode.window.onDidChangeTextEditorSelection(async e => {
if (e.kind === vscode.TextEditorSelectionChangeKind.Mouse) {
if (e.selections[0]) {
await NvUtil.setSelection(e.selections[0]);
}
}
});
vscode.workspace.onDidChangeTextDocument(VscHandlers.handleTextDocumentChange);
// Event to update active configuration items when changed without restarting vscode
vscode.workspace.onDidChangeConfiguration((e: void) => {
Configuration.updateConfiguration();
});
overrideCommand(context, 'type', async args => {
Vim.taskQueue.queueMicroTask(() => {
VscHandlers.handleKeyEventNV(args.text);
});
});
const keysToBind = packagejson.contributes.keybindings;
const ignoreKeys = Configuration.ignoreKeys;
for (let key of keysToBind) {
if (ignoreKeys.all.indexOf(key.vimKey) !== -1) {
continue;
}
vscode.commands.executeCommand('setContext', `vim.use_${key.vimKey}`, true);
registerCommand(context, key.command, () => {
Vim.taskQueue.queueMicroTask(() => {
VscHandlers.handleKeyEventNV(`${key.vimKey}`);
});
});
}
const proc = spawn(
Configuration.neovimPath,
[
// '-u',
// 'NONE',
'-N',
'--embed',
vscode.window.activeTextEditor ? vscode.window.activeTextEditor!.document.fileName : '',
],
{
cwd: __dirname,
}
);
proc.on('error', function (err) {
console.log(err);
vscode.window.showErrorMessage('Unable to setup neovim instance! Check your path.');
});
let nvim: NeovimClient;
if (fs.existsSync('/tmp/nvim') && fs.lstatSync('/tmp/nvim').isSocket()) {
nvim = attach({ socket: '/tmp/nvim' });
Vim.DEBUG = true;
} else {
nvim = attach({ proc: proc });
Vim.DEBUG = false;
}
Vim.nv = nvim;
Vim.channelId = (await nvim.requestApi())[0] as number;
const WIDTH = 50;
const HEIGHT = 36;
nvim.uiAttach(WIDTH, HEIGHT, { ext_cmdline: true, ext_wildmenu: true });
Vim.screen = new Screen({ width: WIDTH, height: HEIGHT });
const code = `
function _vscode_copy_text(text, line, char)
vim.api.nvim_command('undojoin')
vim.api.nvim_buf_set_lines(0, 0, -1, true, text)
vim.api.nvim_call_function('setpos', {'.', {0, line, char, false}})
end
`;
await Vim.nv.lua(code, []);
await nvim.command('autocmd!');
// todo(chilli): Create this map just from RPCHandlers and a decorator.
const autocmdMap: { [autocmd: string]: string } = {
BufWriteCmd: 'writeBuf',
QuitPre: 'closeBuf',
BufEnter: 'enterBuf',
TabNewEntered: 'newTabEntered',
};
for (const autocmd of Object.keys(autocmdMap)) {
await nvim.command(
`autocmd ${autocmd} * :call rpcrequest(${Vim.channelId}, "${autocmdMap[
autocmd
]}", expand("<abuf>"), fnamemodify(expand('<afile>'), ':p'), expand("<afile>"))`
);
}
// Overriding commands to handle them on the vscode side.
// await nvim.command(`nnoremap gd :call rpcrequest(${Vim.channelId},"goToDefinition")<CR>`);
await NvUtil.setSettings(['noswapfile', 'hidden']);
nvim.on('notification', (method: any, args: any) => {
if (vscode.window.activeTextEditor && method === 'redraw') {
Vim.screen.redraw(args);
}
});
nvim.on('request', async (method: string, args: Array<any>, resp: any) => {
if (RpcRequest[method] !== undefined) {
const f = RpcRequest[method];
f(args, resp);
} else {
console.log(`${method} is not defined!`);
}
});
if (vscode.window.activeTextEditor) {
await VscHandlers.handleActiveTextEditorChange();
}
}
function overrideCommand(
context: vscode.ExtensionContext,
command: string,
callback: (...args: any[]) => any
) {
let disposable = vscode.commands.registerCommand(command, async args => {
if (!vscode.window.activeTextEditor) {
return;
}
if (
vscode.window.activeTextEditor.document &&
vscode.window.activeTextEditor.document.uri.toString() === 'debug:input'
) {
await vscode.commands.executeCommand('default:' + command, args);
return;
}
callback(args);
});
context.subscriptions.push(disposable);
}
function registerCommand(
context: vscode.ExtensionContext,
command: string,
callback: (...args: any[]) => any
) {
let disposable = vscode.commands.registerCommand(command, async args => {
if (!vscode.window.activeTextEditor) {
return;
}
callback(args);
});
context.subscriptions.push(disposable);
}
process.on('unhandledRejection', function (reason: any, p: any) {
console.log('Unhandled Rejection at: Promise ', p, ' reason: ', reason);
});