StanAngeloff / php.vim

An up-to-date Vim syntax for PHP (7.x supported)
477 stars 69 forks source link

Target the class name of a fully-qualified class in a "use" statement #85

Closed dstrunk closed 6 years ago

dstrunk commented 6 years ago

I'm attempting to target the class name in a fully qualified class in a use statement for use in a colorscheme (https://github.com/tomasiser/vim-code-dark, for those interested). One key part of the colorscheme is the highlighting of the classname within a namespaced "use" statement, e.g.:

screenshot 2018-10-19 21 58 52

In the above, the targets are App, User, Entry, and Model.

I think I've got the regex required to target this bit of the use statement, but can't figure out how to implement: /\(function\_s)+\)\@!\(\(\\\)\@<=\)\(\h\w*\)\(\(;\|\_s\)\@=\)/

could somebody point me in the right direction for implementing this? I've tried the following underneath the " use statement comment, as well as added phpUseFinalClass to the nextgroup under phpUseClass:

syn match phpUseFinalClass /\(function\_s)+\)\@!\(\(\\\)\@<=\)\(\h\w*\)\(\(;\|\_s\)\@=\)/ contained contains=phpUseNamespaceSeparator

FWIW, I'm aware this regex isn't exhaustive for {} use statements, just trying to get a proof of concept done for a potential PR.

StanAngeloff commented 6 years ago

I'm somewhat confused. You seem to be referencing multiple things. Can you give a code example and surround the bit you want to highlight differently in <<< and >>>, e.g.,:

<?php

namespace App;

<<<final>>> class MyClass
{
}
dstrunk commented 6 years ago

sure (I admit the name phpUseFinalClass needs some tweaking):

<?php

namespace <<<App>>>;

use App\<<<User>>>;
use Illuminate\Database\Eloquent\<<<Model>>>;
use Some\Other\<<<Class>>> as Alias;
use Some\<<<Class>>>, Another\Random\<<<Class>>>;
use One\More\{
    <<<TestClass>>>,
    <<<ForGoodMeasure>>>
};

Edit:

Here's a preliminary PR that will hopefully give a better explanation of what I'm shooting for: https://github.com/dstrunk/php.vim/commit/a4d1ab67d333a966f5e6d97e1dd6a7d589dd52f6

StanAngeloff commented 6 years ago

Okay, second try, GitHub just lost my original, long, comment. Try this:

syn keyword phpInclude namespace contained nextgroup=phpNamespaceName skipwhite skipempty
syn match phpNamespaceName /\(\\\|\h\w*\)*\h\w*/ contained contains=phpUseNamespaceSeparator skipwhite skipempty
syn match phpNamespaceOrUseClassName /\h\w*\(\_s*;\)\@=/ contained containedin=phpUseClass,phpNamespaceName,phpUseAlias
hi phpUseClassName guifg=#00c400

should get you this:

image

dstrunk commented 6 years ago

Awesome, thanks so much! Looks like I was overthinking regex per usual. This should get me pointed in the right direction for some of the other use-cases.

Would you be interested in a PR for this? You can close if not

StanAngeloff commented 6 years ago

This is the kind of thing that can live in the Snippets section of the README. Let me know if you run into any edge cases.