kaarmu / typst.vim

Vim plugin for Typst
MIT License
281 stars 24 forks source link

Color syntactic sugar for `cite` too (`@label`) #96

Closed rikhuijzer closed 4 months ago

rikhuijzer commented 4 months ago

Thanks for making typst.vim. It helps me a lot while editing documents.

I would like to make a feature request and hope this is the right place. Would it be possible to also color citations of the form @somelabel? I currently have colors for function calls like cite(<somelabel>), but not for @somelabel.

kaarmu commented 4 months ago

Thanks for using it!

Hmm, are you sure? References should have highlighting. Could you provide a complete code snippet?

rikhuijzer commented 4 months ago

Hmm, are you sure? References should have highlighting. Could you provide a complete code snippet?

Hmm yes this is what I see

image

so @bloam2020mlj is not highlighted. This is also after disabling all my plugins. Do you know anything I can do to debug this or provide more information?

kaarmu commented 4 months ago

Strange... I see that <label> also has wrong highlighting (no color on the delimiters)

Are you on vim or neovim, and which version? Have you updated the plugin recently?

rikhuijzer commented 4 months ago

Strange... I see that <label> also has wrong highlighting (no color on the delimiters)

Are you on vim or neovim, and which version? Have you updated the plugin recently?

Yes weird

Are you on vim or neovim, and which version?

$ nvim --version
NVIM v0.10.0-dev-091e374
Build type: Release
LuaJIT 2.1.1693350652

Have you updated the plugin recently?

I just did a PlugUpdate while having set Plug 'kaarmu/typst.vim'. It updated something (80 added lines), but I'm not sure what commit exactly. The highlighting looks the same after the update and a Nvim restart.

kaarmu commented 4 months ago

Hmm... I just checked with a nightly build of neovim and it worked fine for me. Would you mind checking the syntstack like this:

  1. Add command to vimrc (or similar)
    nnoremap <F10> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
    \ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
    \ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>
  2. Open your file, position the cursor in the following ways, and call the above command with F10.
    • some text [@]author more text have cursor on @
    • some text @a[u]thor more text have cursor on any letter in the reference
  3. Return with both results.

Also, it seems like labels highlighting is just broken when in code mode. Somehow I'd just missed implementing that until now :upside_down_face: Fixed in a4e7371.

rikhuijzer commented 4 months ago

Hmm... I just checked with a nightly build of neovim and it worked fine for me. Would you mind checking the syntstack like this:

Thanks for the clear instructions and your persistence in debugging this. Log outputs are shown below.

some text [@]author more text have cursor on @

hi<typstMarkupReference> trans<typstMarkupReference> lo<Type>

some text @a[u]thor more text have cursor on any letter in the reference

hi<typstMarkupReference> trans<typstMarkupReference> lo<Type>

I've moved the cursor around a bit and confirmed that both at the @ and at the text give indeed the same output.

Also, it seems like labels highlighting is just broken when in code mode. Somehow I'd just missed implementing that until now 🙃 Fixed in a4e7371.

Nice. Then we achieved at least something in this issue so far 😛

kaarmu commented 4 months ago

Can this perhaps be due to a colorscheme? If you check the Structure group in :highlight, does the xxx have the same highlighting as your reference (i.e. plain normal text)? Here is mine with Vim's default colorscheme.

image

rikhuijzer commented 4 months ago

I'm not sure what the output of :highlight means, but here it is:

afbeelding

afbeelding

afbeelding

So I guess this is the problem? xxx are white for me (like the text)? I guess we can close this issue then if yes.

rikhuijzer commented 4 months ago

The issue persists after resetting :highlight via :highlight clear. I have also double checked my init.vim but don't see obvious causes for the changed highlighting.

```vim call plug#begin() " Plug 'czheo/mojo.vim', {'commit': 'c5898c2e1137cd420524859f209b6831841397e4'} Plug 'editorconfig/editorconfig-vim' Plug 'github/copilot.vim', {'tag': 'v1.28.0'} " Plug 'TabbyML/vim-tabby' Plug 'junegunn/fzf', {'do': { -> fzf#install()}} Plug 'kaarmu/typst.vim' Plug 'neoclide/coc.nvim', {'branch': 'release'} Plug 'neovim/nvim-lspconfig' Plug 'p00f/clangd_extensions.nvim' Plug 'vim-airline/vim-airline' " Has no inlay hints anymore. " Plug 'mrcjkb/rustaceanvim' " Plug 'antiagainst/vim-tablegen' call plug#end() syntax on " Use this setting with `gq` to wrap text. " Disabled because it always auto-wraps. " " Manually set this in Markdown files when needed. set textwidth=0 " Quick map for escaping insert mode. inoremap noremap " Disable automatic wrapping. This doesn't work. set formatoptions-=tc " trigger `autoread` when files changes on disk set autoread autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif " notification after file change autocmd FileChangedShellPost * \ echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None " Touch a central file to trigger watching `enter` instances. autocmd BufWritePost * silent! !echo $(date +\%s) > /Users/rik/git/last_nvim_write.txt let g:slime_target="x11" let g:coc_global_extensions = [ \'coc-clangd', \'coc-pyright', \'coc-rust-analyzer', \] " De-disable Copilot for Markdown and YAML. " Temporarily disable for Typst to make copy pasting papers easier. let g:copilot_filetypes = { \ 'markdown': 1, \ 'yaml': 1, \ 'typst': 0, \ } let g:tabby_keybinding_accept = '' set spelllang=en_us " To see trailing spaces. set list set tabstop=4 set shiftwidth=4 " Related to normal mode indentation commands. set expandtab " Enforce insert spaces and not tabs. set noinsertmode " Disable insert mode. " This appears to have stopped working on newer NeoVim versions? autocmd BufRead,BufNewFile *.jl set filetype=julia autocmd FileType julia setlocal ts=4 sts=4 sw=4 expandtab " To ensure that indentation is the same for Julia and Franklin Markdown. autocmd FileType markdown setlocal ts=4 sts=4 sw=4 expandtab autocmd FileType cpp setlocal ts=2 sts=2 sw=2 expandtab autocmd BufRead,BufNewFile *.td set filetype=TableGen autocmd FileType TableGen setlocal ts=2 sts=2 sw=2 expandtab " Same as in llvm-project/mlir/utils/vim/ftdetect/mlir.vim " For syntax highlighting, see the mlir.vim file. autocmd BufRead,BufNewFile *.mlir set filetype=mlir autocmd FileType mlir setlocal ts=2 sts=2 sw=2 expandtab autocmd FileType javascript setlocal ts=2 sts=2 sw=2 expandtab autocmd FileType json setlocal ts=2 sts=2 sw=2 expandtab autocmd FileType css setlocal ts=2 sts=2 sw=2 expandtab autocmd FileType html setlocal ts=2 sts=2 sw=2 expandtab autocmd FileType sh setlocal ts=2 sts=2 sw=2 expandtab set smartcase " Ignore case if search pattern is lower case. " Shortcuts, see https://vim.fandom.com/wiki/Alternative_tab_navigation " Chosen such that they do not conflict with Zellij. nmap :wa nmap :bd " Unload buffer d(efault). nmap :FZF imap :wa imap :bd imap :FZF nnoremap F :bprevious nnoremap J :bnext " This unsets the 'last search pattern' register by hitting return nnoremap :noh " Enable the list of buffers let g:airline#extensions#tabline#enabled = 1 " Straight tabs let g:airline#extensions#tabline#left_sep = ' ' let g:airline#extensions#tabline#left_alt_sep = '|' " FZF include hidden let $FZF_DEFAULT_COMMAND = 'rg -l ""' " " BEGIN COC.NVIM CONFIGURATION " " Plug 'neoclide/coc.nvim', {'do': 'yarn install --frozen-lockfile'} set shell=/bin/sh " Without this TextEdit might fail, and we cannot switch buffers without save. set hidden " Some servers have issues with backup files, see #649 set nobackup set nowritebackup " Better display for messages set cmdheight=2 " You will have bad experience for diagnostic messages when it's default 4000. set updatetime=300 " don't give |ins-completion-menu| messages. set shortmess+=c " always show signcolumns set signcolumn=yes " Use tab for trigger completion with characters ahead and navigate. " Use command ':verbose imap ' to make sure tab is not mapped by other plugin. inoremap \ pumvisible() ? "\" : \ check_back_space() ? "\" : \ coc#refresh() inoremap pumvisible() ? "\" : "\" function! s:check_back_space() abort let col = col('.') - 1 return !col || getline('.')[col - 1] =~# '\s' endfunction " Use to trigger completion. inoremap coc#refresh() " Use to confirm completion, `u` means break undo chain at current position. " Coc only does snippet and additional edit on confirm. inoremap pumvisible() ? "\" : "\u\" " Use `[c` and `]c` to navigate diagnostics nmap [c (coc-diagnostic-prev) nmap ]c (coc-diagnostic-next) " Remap keys for gotos nmap gd (coc-definition) nmap gy (coc-type-definition) nmap gi (coc-implementation) nmap gr (coc-references) " Use K to show documentation in preview window nnoremap K :call show_documentation() function! s:show_documentation() if (index(['vim','help'], &filetype) >= 0) execute 'h '.expand('') else call CocAction('doHover') endif endfunction " Highlight symbol under cursor on CursorHold autocmd CursorHold * silent call CocActionAsync('highlight') " Remap for rename current word nmap rn (coc-rename) " Remap for format selected region xmap f (coc-format-selected) nmap f (coc-format-selected) augroup mygroup autocmd! " Update signature help on jump placeholder autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp') augroup end " Remap for do codeAction of selected region, ex: `aap` for current paragraph xmap a (coc-codeaction-selected) nmap a (coc-codeaction-selected) " Remap for do codeAction of current line nmap ac (coc-codeaction) " Fix autofix problem of current line nmap qf (coc-fix-current) " Use for select selections ranges, needs server support, like: coc-tsserver, coc-python nmap (coc-range-select) xmap (coc-range-select) xmap (coc-range-select-backword) " Use `:Format` to format current buffer command! -nargs=0 Format :call CocAction('format') " Use `:Fold` to fold current buffer command! -nargs=? Fold :call CocAction('fold', ) " use `:OR` for organize import of current buffer command! -nargs=0 OR :call CocAction('runCommand', 'editor.action.organizeImport') " Add status line support, for integration with other plugin, checkout `:h coc-status` " Removed since it was incompatible with Nix syntax. " Using CocList " Show all diagnostics nnoremap a :CocList diagnostics " Manage extensions nnoremap e :CocList extensions " Show commands nnoremap c :CocList commands " Find symbol of current document nnoremap o :CocList outline " Search workspace symbols nnoremap s :CocList -I symbols " Do default action for next item. nnoremap j :CocNext " Do default action for previous item. nnoremap k :CocPrev " Resume latest coc list nnoremap p :CocListResume " Force disable automatic indentation. :setl inde= " lua << EOF " require'lspconfig'.julials.setup{} " EOF lua << EOF local lspconfig = require('lspconfig') lspconfig.mojo.setup{} local function on_attach(client, bufnr) require("clangd_extensions.inlay_hints").setup_autocmd() require("clangd_extensions.inlay_hints").set_inlay_hints() end lspconfig.clangd.setup({ on_attach = on_attach }) lspconfig.rust_analyzer.setup({ on_attach = function(client, bufnr) vim.lsp.inlay_hint.enable(bufnr) end, settings = { ["rust-analyzer"] = { inlayHints = { typeHints = true, parameterHints = true, chainingHints = false, maxLength = 15, } } } }) EOF " " END COC.NVIM CONFIGURATION " ```

I have removed the plugs (especially vim-airline) but the colors remain the same.

kaarmu commented 4 months ago

Yes, your problem originates from somewhere else, but we can try to debug it (I'll close the issue though).

I would start by checking if :highlight Structure is still white when you're running barebones nvim -u NORC. It's also good to confirm that you don't have other init scripts in ~/.vim, ~/.config/nvim, etc. that change highlighting.

Also, did you confirm that you were using default highlighting?

Just to mention this as well, if linking to Structure does not work then we can have a discussion about switching highlighting group we inherit from. However, then we should open a new issue for that specific topic.

rikhuijzer commented 4 months ago

but we can try to debug it (I'll close the issue though).

I'm impressed by your persistence!

I would start by checking if :highlight Structure is still white when you're running barebones nvim -u NORC. It's also good to confirm that you don't have other init scripts in ~/.vim, ~/.config/nvim, etc. that change highlighting.

I've checked it and nvim -u NORC doesn't change anything. Still the same. Also, I had no other init scripts.

Also, did you confirm that you were using default highlighting?

I followed https://stackoverflow.com/questions/2419624, by running :echo g:colors_name which returned a E121. According to that post, this means that I have the default color scheme set. Running :colorscheme default did not change my colors meaning that I did not have a malformed color scheme.

As a test, I changed to the tokyonight color scheme, which is a popular one according to Reddit, with

colorscheme tokyonight

and now the highlighting indeed does work as expected! 🚀

afbeelding

Just to mention this as well, if linking to Structure does not work then we can have a discussion about switching highlighting group we inherit from. However, then we should open a new issue for that specific topic.

I guess whether that is required is best judged by you. The idea would make sense to me, since it looks like it could fix the issue for other people with default configs, but I'm not sure how "default" my config really is.

kaarmu commented 4 months ago

Great! If you are happy with non-default colorschemes then it is resolved. Since it doesn't seem like you get normal highlighting for Structure I'd say we hold on switching parent group. Though if it comes up again...

rikhuijzer commented 4 months ago

Sounds good! I now switched to another colorscheme and all is working well for me.

Thanks for the help @kaarmu! Much appreciated! I learned a lot about nvim throughout this conversation