vim-cpp icon indicating copy to clipboard operation
vim-cpp copied to clipboard

Support User defined literals

Open osyo-manga opened this issue 10 years ago • 6 comments

  • 関連:https://twitter.com/Linda_pp/status/444140782639804416

Twitter でそういう話がでていたので立てておきます。

[目的]

以下のように C++11 で追加されたユーザ定義リテラルもリテラルとして一緒に ハイライトを行いたい。

// _user_defined_literal をリテラルとしてハイライトしたい
1234_user_defined_literal
"homu"_user_defined_literal

Vim のシンタックスはあまり詳しくないのですが、とりあえず、既存のシンタックスの末尾に `\w*` を追加してみました。
syn match   cNumber     display contained "\d\+\(\w*\)\>"
syn match   cNumber     display contained "0x\x\+\(\w*\)\>"
syn match   cOctal      display contained "0\o\+\(\w*\)\>" contains=cOctalZero
syn match   cFloat      display contained "\d\+\.\d*\(\w*\)"
syn region cCppString   start=+L\="+ skip=+\\\\\|\\"\|\\$+ excludenl end=+"\w*+ end='$' contains=cSpecial,cFormat,@Spell

いまは『数値+リテラル』を1つのシンタックスとして定義する事を考えています。 上記のコードを https://github.com/vim-jp/cpp-vim/blob/master/syntax/cpp.vim#L38 あたりに追加すればよさそう? Vim のシンタックスに詳しい人誰か。

osyo-manga avatar Mar 13 '14 17:03 osyo-manga

リテラルとして扱うのではなく、どうせならユーザ定義リテラルだと分かる様にしたいです。

mattn avatar Mar 15 '14 14:03 mattn

反応遅くなってすみません.

とりあえず上ので実装して試してみました. https://github.com/rhysd/cpp-vim/tree/user-defined-literals

ss 少し試した感じだと,ちゃんと動いているっぽいです.

@mattn その場合,ユーザ定義リテラル用のハイライトを定義して,nextgroup を既存のリテラルのハイライトに定義するのが良いんでしょうか? ハイライト設定はあまり書いたことが無いので,どうやるのが定石なのか分からないです…

rhysd avatar Mar 15 '14 16:03 rhysd

ですです > nextgroup

mattn avatar Mar 15 '14 16:03 mattn

うーん,現状 nextgroup で単純にやろうとすると,数値リテラルはすべて \> で終わっているので,45min をハイライトできないですね… 何か良い方法あるでしょうか?

rhysd avatar Mar 15 '14 18:03 rhysd

Hi all, any plans to add literals highlight into master?

artditel avatar Nov 16 '15 11:11 artditel

リテラルとして扱うのではなく、どうせならユーザ定義リテラルだと分かる様にしたいです。 I do not want to treat it as a literal, but I want to make sure that it is a user-defined literal.

@osyo-manga:

My understanding is that to do this would require information from the source file which can only be obtained by parsing it as a compiler would do. Consider the related desire of highlighting macros: it would be necessary to detect if a sequence was found in a corresponding preprocessor directive, which involves scanning the file for those include directives.

While it is possible to solve the above example fairly easily, this quickly requires implementing more and more rules until, eventually, you must implement a C++ parser to get highlighting right in every case. And it is not possible to do this with regular expressions.

I would suggest closing this issue and researching e.g. DyeVim to see if it fits your needs, or another project found by a search query like "vim C++ semantic highlighting".

ghost avatar May 14 '17 21:05 ghost