dart-lang/dart-vim-plugin

Improve syntax highlighting (types)

lleaff opened this issue · 5 comments

Currently, there are no syntax rules to detect type annotations, other than a list of built-ins (int, String, etc).
It would be nice if any value used as a type could be highlighted, either in function arguments, return types, or variable declarations.

Here is a comparison screenshot, Vim with dart-vim-plugin on the left, VS Code with the Dart plugin on the right:
image

For instance, in the line with: Widget build(BuildContext context) {,
Widget and BuildContext should be highlighted differently than context.

VS Code is getting highlights based on semantic information from the analysis server. We don't have a way to do that. (In theory an LSP plugin could do something here, but it's not easy in vim. There has been talk about making it easier but even if we get nice APIs in vim we probably would push that to an LSP plugin instead of implementing it here).

We could maybe make assumptions about semantics, like assuming that everything that starts with a capital letter is a type. It would fall down with non-idiomatic code, but maybe that isn't a problem?

Agreed that making assumptions about capital or underscore-capital identifiers feels a bit dodgy.

Playing devil's advocate for a bit, it's worth noting that we already make certain assumptions about code being idiomatic -- e.g. we highlight async as a keyword, even when used as a local variable, which the spec (section 20.1.1) allows, since it's not a reserved word.

That said, I have no firm opinion one way or another. My general inclination would be to ask what other similarly-lightweight vim language plugins do.

I played around a bit with the built in java config and it behaves similarly to what we have today.

I also tried highlighting capitalized names as Type and it feel very heavy. It may be partially due to my colorscheme using something bright for Type, but I also don't like that it makes things like class definition lines look monotone - "class" and "extends" already highlight as Typedef which also falls back to Type by default.

@lleaff - if you want to propose something that you like we can consider it, but I'm not able to find something that I think is an improvement over what we have today...

I agree that the long term solution is to have that provided by the language server as in VS Code. Tracking those issues in the major clients:
autozimu/LanguageClient-neovim#383
prabirshrestha/vim-lsp#198

The uppercase assumption seems like a smart temporary solution ! I played around a little bit with those rules:

syntax match   dartType           "\<[A-Z][A-Za-z0-9_]*\>"
syntax match   dartConstant       "\<[A-Z][A-Z0-9_]\+\>"

Of course as you pointed out you then need to differentiate between Type, Typedef and StorageClass in your theme.

Personally I think it's a definite improvement, without it I find any language with C-style type annotations unreadable.

Currently, there are no syntax rules to detect type annotations, other than a list of built-ins (int, String, etc).
It would be nice if any value used as a type could be highlighted, either in function arguments, return types, or variable declarations.

Here is a comparison screenshot, Vim with dart-vim-plugin on the left, VS Code with the Dart plugin on the right:
image

For instance, in the line with: Widget build(BuildContext context) {,
Widget and BuildContext should be highlighted differently than context.

how are you,I'm a chinese engerineer,I see your picture you put here seems the plugin runs well.but it doesn't highlight my dartlang syntax.could you please paste your settings about dartlang in your .vimrc?I need your help, thanks!