Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to vscode-neovim #34

Closed
pomdtr opened this issue Dec 9, 2019 · 10 comments
Closed

Adapt to vscode-neovim #34

pomdtr opened this issue Dec 9, 2019 · 10 comments

Comments

@pomdtr
Copy link

pomdtr commented Dec 9, 2019

Vscode neovim allow to use vim inside a vscode window. Mapping of vscode action is supported directly in the init.vim file. A port of this configuration should be easy.

https://github.com/asvetliakov/vscode-neovim

@pomdtr
Copy link
Author

pomdtr commented Dec 21, 2019

For example, here is my init.vim

" Vscode Commands
if exists('g:vscode')
	function! s:VisualCommand(commandString)
			normal! gv
			call VSCodeNotify(a:commandString)
	endfunction

	xnoremap <silent> gh :<C-u>call <SID>hover()<CR>

	nnoremap <silent> <Leader><space> :<C-u>call VSCodeNotify('workbench.action.showCommands')<CR>
	nnoremap <silent> <Leader>pf :<C-u>call VSCodeNotify('workbench.action.quickOpen')<CR>
	nnoremap <silent> <Leader>/ :<C-u>call VSCodeNotify('actions.find')<CR>
	nnoremap <silent> <Leader>= :<C-u>call VSCodeNotify('editor.action.formatDocument')<CR>
	nnoremap <silent> <Leader>bd :<C-u>call VSCodeNotify('workbench.action.closeActiveEditor')<CR>
	nnoremap <silent> <Leader>bo :<C-u>call VSCodeNotify('workbench.action.closeOtherEditors')<CR>
	nnoremap <silent> <Leader>br :<C-u>call VSCodeNotify('workbench.action.files.revert')<CR>
	nnoremap <silent> <Leader>bs :<C-u>call VSCodeNotify('workbench.action.files.newUntitledFile')<CR>
	nnoremap <silent> <Leader>el :<C-u>call VSCodeNotify('workbench.actions.view.problems')<CR>
	nnoremap <silent> <Leader>ep :<C-u>call VSCodeNotify('editor.action.marker.previous')<CR>
	nnoremap <silent> <Leader>en :<C-u>call VSCodeNotify('editor.action.marker.next')<CR>
	nnoremap <silent> <Leader>f/ :<C-u>call VSCodeNotify('actions.find')<CR>
	nnoremap <silent> <Leader>f2 :<C-u>call VSCodeNotify('fileutils.duplicateFile')<CR>
	nnoremap <silent> <Leader>fd :<C-u>call VSCodeNotify('fileutils.removeFile')<CR>
	nnoremap <silent> <Leader>fe :<C-u>call VSCodeNotify('workbench.action.openGlobalSettings')<CR>
	nnoremap <silent> <Leader>fE :<C-u>call VSCodeNotify('workbench.action.openSettingsJson')<CR>
	nnoremap <silent> <Leader>fh :<C-u>call VSCodeNotify('gitlens.showQuickFileHistory')<CR>
	nnoremap <silent> <Leader>fm :<C-u>call VSCodeNotify('fileutils.moveFile')<CR>
	nnoremap <silent> <Leader>fn :<C-u>call VSCodeNotify('fileutils.newFileAtRoot')<CR>
	nnoremap <silent> <Leader>fo :<C-u>call VSCodeNotify('workbench.action.files.openFile')<CR>
	nnoremap <silent> <Leader>fO :<C-u>call VSCodeNotify('workbench.action.files.openFile')<CR>
	nnoremap <silent> <Leader>fr :<C-u>call VSCodeNotify('fileutils.renameFile')<CR>
	nnoremap <silent> <Leader>fs :<C-u>call VSCodeNotify('workbench.action.gotoSymbol')<CR>
	nnoremap <silent> <Leader>fw :<C-u>call VSCodeNotify('workbench.action.files.save')<CR>
	nnoremap <silent> <Leader>fy :<C-u>call VSCodeNotify('workbench.action.files.copyPathOfActiveFile')<CR>
	nnoremap <silent> <Leader>gb :<C-u>call VSCodeNotify('gitlens.toggleFileBlame')<CR>
	nnoremap <silent> <Leader>gh :<C-u>call VSCodeNotify('gitlens.showQuickRepoHistory')<CR>
	nnoremap <silent> <Leader>gs :<C-u>call VSCodeNotify('workbench.view.scm')<CR>
	nnoremap <silent> <Leader>hd :<C-u>call VSCodeNotify('workbench.action.openGlobalKeybindings')<CR>
	nnoremap <silent> <Leader>hD :<C-u>call VSCodeNotify('workbench.action.openGlobalKeybindingsFile')<CR>
	nnoremap <silent> <Leader>pf :<C-u>call VSCodeNotify('workbench.action.quickOpen')<CR>
	nnoremap <silent> <Leader>pl :<C-u>call VSCodeNotify('workbench.action.files.openFolder')<CR>
	nnoremap <silent> <Leader>pp :<C-u>call VSCodeNotify('workbench.action.openRecent')<CR>
	nnoremap <silent> <Leader>ps :<C-u>call VSCodeNotify('workbench.action.showAllSymbols')<CR>
	nnoremap <silent> <Leader>qq :<C-u>call VSCodeNotify('workbench.action.closeWindow')<CR>
	nnoremap <silent> <Leader>qr :<C-u>call VSCodeNotify('workbench.action.reloadWindow')<CR>
	nnoremap <silent> <Leader>rn :<C-u>call VSCodeNotify('search.action.focusNextSearchResult')<CR>
	nnoremap <silent> <Leader>rp :<C-u>call VSCodeNotify('search.action.focusPreviousSearchResult')<CR>
	nnoremap <silent> <Leader>rs :<C-u>call VSCodeNotify('workbench.action.findInFiles')<CR>
	nnoremap <silent> <Leader>rn :<C-u>call VSCodeNotify('editor.action.rename')<CR>
	nnoremap <silent> <Leader>ta :<C-u>call VSCodeNotify('workbench.action.toggleActivityBarVisibility')<CR>
	nnoremap <silent> <Leader>tb :<C-u>call VSCodeNotify('workbench.action.toggleSidebarVisibility')<CR>
	nnoremap <silent> <Leader>tc :<C-u>call VSCodeNotify('workbench.action.toggleCenteredLayout')<CR>
	nnoremap <silent> <Leader>td :<C-u>call VSCodeNotify('workbench.view.debug')<CR>
	nnoremap <silent> <Leader>te :<C-u>call VSCodeNotify('workbench.view.explorer')<CR>
	nnoremap <silent> <Leader>tf :<C-u>call VSCodeNotify('workbench.action.toggleFullScreen')<CR>
	nnoremap <silent> <Leader>tg :<C-u>call VSCodeNotify('workbench.view.scm')<CR>
	nnoremap <silent> <Leader>t/ :<C-u>call VSCodeNotify('workbench.action.findInFiles')<CR>
	nnoremap <silent> <Leader>tl :<C-u>call VSCodeNotify('workbench.view.extension.gitlens')<CR>
	nnoremap <silent> <Leader>tm :<C-u>call VSCodeNotify('workbench.action.toggleMenuBar')<CR>
	nnoremap <silent> <Leader>tM :<C-u>call VSCodeNotify('editor.action.toggleMinimap')<CR>
	nnoremap <silent> <Leader>tp :<C-u>call VSCodeNotify('workbench.action.togglePanel')<CR>
	nnoremap <silent> <Leader>' :<C-u>call VSCodeNotify('workbench.action.terminal.toggleTerminal')<CR>
	nnoremap <silent> <Leader>ts :<C-u>call VSCodeNotify('workbench.action.selectTheme')<CR>
	nnoremap <silent> <Leader>tx :<C-u>call VSCodeNotify('workbench.view.extensions')<CR>
	nnoremap <silent> <Leader>tz :<C-u>call VSCodeNotify('workbench.action.toggleZenMode')<CR>
	nnoremap <silent> <Leader>v :<C-u>call VSCodeNotify('editor.action.smartSelect.grow')<CR>
	nnoremap <silent> <Leader>V :<C-u>call VSCodeNotify('editor.action.smartSelect.shrink')<CR>
	nnoremap <silent> <Leader>w- :<C-u>call VSCodeNotify('workbench.action.splitEditorDown')<CR>
	nnoremap <silent> <Leader>w/ :<C-u>call VSCodeNotify('workbench.action.splitEditorRight')<CR>
	nnoremap <silent> <Leader>wd :<C-u>call VSCodeNotify('workbench.action.closeEditorsInGroup')<CR>
	nnoremap <silent> <Leader>wh :<C-u>call VSCodeNotify('workbench.action.focusLeftGroup')<CR>
	nnoremap <silent> <Leader>wH :<C-u>call VSCodeNotify('workbench.action.moveActiveEditorGroupLeft')<CR>
	nnoremap <silent> <Leader>wj :<C-u>call VSCodeNotify('workbench.action.focusBelowGroup')<CR>
	nnoremap <silent> <Leader>wJ :<C-u>call VSCodeNotify('workbench.action.moveActiveEditorGroupDown')<CR>
	nnoremap <silent> <Leader>wk :<C-u>call VSCodeNotify('workbench.action.focusAboveGroup')<CR>
	nnoremap <silent> <Leader>wK :<C-u>call VSCodeNotify('workbench.action.moveActiveEditorGroupUp')<CR>
	nnoremap <silent> <Leader>wl :<C-u>call VSCodeNotify('workbench.action.focusRightGroup')<CR>
	nnoremap <silent> <Leader>wL :<C-u>call VSCodeNotify('workbench.action.moveActiveEditorGroupRight')<CR>
	nnoremap <silent> <Leader>wm :<C-u>call VSCodeNotify('workbench.action.toggleEditorWidths')<CR>
	nnoremap <silent> <Leader>wo :<C-u>call VSCodeNotify('workbench.action.closeEditorsInOtherGroups')<CR>
	nnoremap <silent> <Leader>ws :<C-u>call VSCodeNotify('workbench.action.splitEditorDown')<CR>
	nnoremap <silent> <Leader>wv :<C-u>call VSCodeNotify('workbench.action.splitEditorRight')<CR>
	nnoremap <silent> <Leader>ww :<C-u>call VSCodeNotify('workbench.action.focusNextGroup')<CR>
	nnoremap <silent> <Leader>wW :<C-u>call VSCodeNotify('workbench.action.focusPreviousGroup')<CR>
	nnoremap <silent> <Leader>xw :<C-u>call VSCodeNotify('editor.action.trimTrailingWhitespace')<CR>
	nnoremap <silent> go :<C-u>call VSCodeNotify('editor.action.showContextMenu')<CR>

	xnoremap <silent> <Leader><space> :<C-u>call <SID>VisualCommand('workbench.action.showCommands')<CR>
	xnoremap <silent> > :<C-u>call <SID>VisualCommand("editor.action.indentLines")<CR>
	xnoremap <silent> < :<C-u>call <SID>VisualCommand("editor.action.outdentLines")<CR>
	xnoremap <silent> go :<C-u>call <SID>VisualCommand("editor.action.showContextMenu")<CR>
	nnoremap <silent> <Leader>/ :<C-u>call <SID>VisualCommand('actions.find')<CR>

	xmap gc  <Plug>VSCodeCommentary
	nmap gc  <Plug>VSCodeCommentary
	omap gc  <Plug>VSCodeCommentary
	nmap gcc <Plug>VSCodeCommentaryLine
endif

@StreakyCobra
Copy link
Member

Hi :) I am not using neovim so I am not going to implement, support or test this. If someone is willing to do a PR and support the feature this would be welcome 😊

In the meantime I'm closing the issue because there is not much to do.

@iansinnott
Copy link

Is VSpaceCode tightly coupled to the Vim plugin? Clearly we need a Vim layer to emulate Spacemacs, but do we need that specific vim layer?

Perhaps vscode-neovim could provide the vim mappings in place of the vim plugin.

@macintacos
Copy link
Contributor

@iansinnott I happen to be subscribed to this issue for some reason, but I think a lot has changed since this issue was opened. VSpaceCode (and thus, vscode-which-key) doesn't need any Vim emulation in order to function anymore, as far as I can tell -- it's just an extension that you can invoke. While it's surely a better experience that way, all you need to do is remap the extension's invocation to SPC when you're in the editor and you should be good to go.

With the above in mind, I recommend checking out the README for this project again. One note to call out from "Project History":

This project was initially started by StreakyCobra as a configuration file for VSCodeVim to be merged in your settings.json file. stevenguh developed on his side an extension also with the goal to brings Spacemacs bindings to VSCode. After some discussion we agreed to merge the two projects to keep this niche community unified. stevenguh solution was technically better, and this project had a larger community and was better referenced, so it was decided move his extension here.

Hope that helps.

@iansinnott
Copy link

Thank you @macintacos.

For anyone else coming across this issue I'd point out that using vscode-neovim the <space> key will be sent to neovim, so you can remap it within your init.vim file:

  " Integrate with whichkey for spacemacs-style space key
  nnoremap <space> :call VSCodeNotify('whichkey.show')<CR>

@unvalley
Copy link

You can also override space binding to work vspacecode.space at keybindings.json.

 {
     "key": "space",
     "command": "vspacecode.space",
     "when": "editorTextFocus && neovim.mode == normal"
 },

@a690700752
Copy link

for visual mode, can use this to remap in your lua config file.

local function getVisualSelection()
	local start = vim.fn.getpos("v")
	local finish = vim.fn.getcurpos()
	return start, finish
end

local function exit_visual_mode()
	vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true)
end

vim.keymap.set("n", "<space>", ":call VSCodeNotify('vspacecode.space')<CR>")
vim.keymap.set("x", "<space>", function()
	local start, finish = getVisualSelection()
	if vim.fn.visualmode() == "V" then
		vim.fn.VSCodeNotifyRange("vspacecode.space", start[2], finish[2], 1)
	else
		vim.fn.VSCodeNotifyRangePos("vspacecode.space", start[2], finish[2], start[3], finish[3], 1)
	end
	exit_visual_mode()
end)

@aveaves
Copy link

aveaves commented Sep 11, 2023

for visual mode, can use this to remap in your lua config file.

local function getVisualSelection()
	local start = vim.fn.getpos("v")
	local finish = vim.fn.getcurpos()
	return start, finish
end

local function exit_visual_mode()
	vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true)
end

vim.keymap.set("n", "<space>", ":call VSCodeNotify('vspacecode.space')<CR>")
vim.keymap.set("x", "<space>", function()
	local start, finish = getVisualSelection()
	if vim.fn.visualmode() == "V" then
		vim.fn.VSCodeNotifyRange("vspacecode.space", start[2], finish[2], 1)
	else
		vim.fn.VSCodeNotifyRangePos("vspacecode.space", start[2], finish[2], start[3], finish[3], 1)
	end
	exit_visual_mode()
end)

Could you check if this still works? I tried to toggle comment a visual selection but only the last line of the selection gets commented. That is, I selected a region by "v" using vscode-neovim, pressed "space + ;" to toggle comment of region. Only the last line gets toggled.

For comparison, I successfully toggled the comment of a region by directly invoking the "Toggle Line Comment" command in vscode for the region selected using vscode-neovim visual selection "v".

@a690700752
Copy link

@aveaves The VSCodeNotifyRange API has changed, try this

local function getVisualSelection()
	local start = vim.fn.getpos("v")
	local finish = vim.fn.getcurpos()
	return start, finish
end

vim.keymap.set("n", "<space>", ":call VSCodeNotify('vspacecode.space')<CR>")
vim.keymap.set("x", "<space>", function()
	local start, finish = getVisualSelection()
	if vim.fn.mode() == "V" then
		require("vscode-neovim").notify_range("vspacecode.space", start[2], finish[2], 1)
	else
		require("vscode-neovim").notify_range_pos("vspacecode.space", start[2], finish[2], start[3], finish[3], 1)
	end
end)

@aveaves
Copy link

aveaves commented Sep 11, 2023

@a690700752 Thank you this also works. I solved it a while ago using your first code by removing the exit_visual_mode() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants