Skip to content

Commit

Permalink
Normalize the vim sample
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdima committed Nov 8, 2018
1 parent cbd57bf commit 0460228
Show file tree
Hide file tree
Showing 15 changed files with 586 additions and 388 deletions.
8 changes: 7 additions & 1 deletion .scripts/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ const samples = [{
apis: ['window.createTreeView', 'window.registerTreeDataProvider', 'TreeView', 'TreeDataProvider'],
contributions: ['views', 'viewsContainers']
},
// { description: 'vim-sample', path: 'vim-sample', guide: null, apis: [], contributions: [] },
{
description: 'vim-sample',
path: 'vim-sample',
guide: null,
apis: [`commands`, `StatusBarItem`, `window.createStatusBarItem`, `TextEditorCursorStyle`, `window.activeTextEditor`, `Position`, `Range`, `Selection`, `TextEditor`, `TextEditorRevealType`, `TextDocument`],
contributions: []
},
// { description: 'webpack-sample', path: 'webpack-sample', guide: null, apis: [], contributions: [] },
];

Expand Down
7 changes: 7 additions & 0 deletions vim-sample/.vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"eg2.tslint"
]
}
21 changes: 4 additions & 17 deletions vim-sample/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
// A launch configuration that compiles the extension and then opens it inside a new window
{
"version": "0.1.0",
"version": "0.2.0",
"configurations": [
{
"name": "Launch Extension",
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/src/**/*.js"],
"preLaunchTask": "npm: watch"
},
{
"name": "Launch Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/test/**/*.js"],
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
"outFiles": ["${workspaceFolder}/out/**/*.js"],
"preLaunchTask": "npm: watch"
}
]
Expand Down
13 changes: 1 addition & 12 deletions vim-sample/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
// Place your settings in this file to overwrite default and user settings.
{
"files.exclude": {
"out": false // set this to true to hide the "out" folder with the compiled JS files
},
"files.eol": "\n",
"files.trimTrailingWhitespace": true,
"search.exclude": {
"out": true // set this to false to include "out" folder in search results
},
// Turn off tsc task auto detection since we have the necessary task as npm scripts
"typescript.tsc.autoDetect": "off",
"typescript.tsdk": "./node_modules/typescript/lib" // we want to use the TS server from our node_modules folder to control its version
"editor.insertSpaces": false,
}
18 changes: 6 additions & 12 deletions vim-sample/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
# VSCode vim sample

This is an extension sample showing how vim emulation could be implemented in a VS Code extension, starting with VS Code version 0.10.12.
This is an extension sample showing how vim emulation could be implemented in a VS Code extension.

![Screenshot](example.gif)

## Why create this sample and not contribute to an existing vim extension?
The reason for creating this sample was to figure out the APIs missing in VS Code in order to be able to create a good vim emulation experience.
There are already multiple [vim extensions for VS Code](https://marketplace.visualstudio.com/search?term=vim&target=VSCode&sortBy=Relevance) and I don't want to send mixed signals by picking one to contribute to.
And it's fun to write an extension, even if it's just a sample! :)


## Approach (what is new in 0.10.12)
## Approach

### Typing

* When a key is pressed, VS Code gets a `keydown` event. The `keydown` event contains information about modifier keys (`ctrl`, `alt`, etc.) and about the key code. The `keydown` event does not contain information about what character would get produced and not all `keydown` events produce characters.
* The `keydown` event runs through the keybinding rules and the first rule that matches gets executed. This allows to bind commands to key combinations that would usually not produce visual characters.
* An extension should generally not add rules for key combinations that might produce characters (e.g. don't add rules for `"a"`, `"shift+a"` or `"ctrl+alt+o"`, as these key combinations will not necessarily produce the same characters under different keyboard layouts).
* When a `keydown` is not matched by any keybinding rule, it might produce a character. Since 0.10.12 this is dispatched to the `type` command.
* When a `keydown` is not matched by any keybinding rule, it might produce a character. This is dispatched to the `type` command.
* It is therefore possible for an extension to **overwrite** the `type` command and handle characters instead of the VS Code editor.
* There is a `default:type` command that maps to the VS Code editor's type handler in case an extension wishes to delegate back typing to VS Code.
* There is another command, `replacePreviousChar` that you should be aware of. On the Mac if I remember correctly, it is possible to generate certain accented characters by long pressing a key. When this happens, a `replacePreviousChar` command is invoked containing the new characters and the number of characters that should be replaced before.
* There is another command, `replacePreviousChar` that you should be aware of. On the Mac it is possible to generate certain accented characters by long pressing a key. When this happens, a `replacePreviousChar` command is invoked containing the new characters and the number of characters that should be replaced before.
* At this time, `type`, `replacePreviousChar`, `paste` and `cut` are dispatched through the keybinding rules. There are [other internal VS Code editor commands](https://github.com/Microsoft/vscode/blob/master/src/vs/editor/browser/view/viewController.ts) (such as those coming in from mouse operations) that are not dispatched and cannot be at this time overwritten from an extension. From writing this sample, I haven't found the need to overwrite them, but if you have a use-case where that would make sense, please open an issue on the vscode repo.

### Cursor Style

* The `TextEditorOptions` now also has a `cursorStyle` property that allows to change the cursor style programmatically.
* The `TextEditorOptions` has a `cursorStyle` property that allows to change the cursor style programmatically.

### Keybinding contexts

* There is a new command, `setContext`, that can be invoked with two arguments, a key and a value. It allows to add custom properties to the keybinding conditions.


## Take it for a spin
## Running the Sample

> Note: You need to run from VS Code source, as the 0.10.12 version is not yet released.
Expand Down
Loading

0 comments on commit 0460228

Please sign in to comment.