Skip to content
This repository was archived by the owner on Dec 1, 2020. It is now read-only.

Allow Whitespace Before Heredoc / Nowdoc Block Endings #95

Merged
merged 5 commits into from
Feb 4, 2020
Merged

Allow Whitespace Before Heredoc / Nowdoc Block Endings #95

merged 5 commits into from
Feb 4, 2020

Conversation

eknowlton
Copy link

@eknowlton eknowlton commented Oct 25, 2019

Because:

This commit:

Because:
- In PHP >= 7.3 Whitespace is allowed before the ending/closing tag of a herdoc/nowdoc
- https://www.php.net/manual/en/migration73.new-features.php#migration73.new-features.core.heredoc
- Currently `php.vim` only catches closing tags without whitespace
before

This commit:
- Changes the end tag delimeter, per @weirdan fix
@eknowlton
Copy link
Author

Fixes #89

@eknowlton eknowlton changed the title Allow Whitespace Before Heredoc / Nowdoc Blocks Allow Whitespace Before Heredoc / Nowdoc Block Endings Oct 25, 2019
@StanAngeloff
Copy link
Owner

Where you have version >= 730 this is actually checking against Vim's version not PHP. Was this the intention?

syntax/php.vim Outdated
@@ -670,7 +670,23 @@ endif
syn case match

" HereDoc
if version >= 704
if version >= 730
Copy link
Owner

@StanAngeloff StanAngeloff Oct 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will check against Vim's version, try :echo version<CR> to confirm.

Copy link
Author

@eknowlton eknowlton Oct 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, i just guessed really. I don't have any idea really how to add this. Is there a way to check for the PHP version or should I just update the end search?

@eknowlton
Copy link
Author

So I removed the newest version check and updated the end tag in both versions.

@Taluu
Copy link

Taluu commented Dec 7, 2019

Maybe adding a variable config to activate or disable this would be the way to go ? As it's done for the html and sql in strings.

(this is a hidden ping :D)

@StanAngeloff
Copy link
Owner

Thanks, GitHub notifications are pretty bad on mobile, looks like an overlook on my part. Yes, a configurable flag would be ideal with defaults to current behaviour?

@eknowlton
Copy link
Author

I'd like to adhere but I don't understand which feature would be configurable.

Would it enable/disable the ability to include whitespace in HEREDOC/NOWDOC's?

Something along the lines of php_whitespace_in_heredoc_nowdoc that defaults to 1?

@StanAngeloff
Copy link
Owner

How about we add a buffer/global variable that specifies the PHP version? Features can then be on/off based on that variable?

@Taluu
Copy link

Taluu commented Dec 9, 2019

We could work on the PHP_VERSION_ID then (70400 for 7.4.0, and so on), defaulting to the latest ?

@eknowlton
Copy link
Author

That seems like a great idea. I believe I initially thought that was how it worked. :)

@eknowlton
Copy link
Author

If I go ahead and add PHP_VERSION_ID in this PR, should I address other features, or just this one for now?

@StanAngeloff
Copy link
Owner

In line with the rest of the variables in the syntax, this should be all lowercase php_version_id. I like the idea of following the official constant PHP_VERSION_ID as per @Taluu suggestion (70400 for 7.4.0, and so on).

@eknowlton
Copy link
Author

The idea to enable this feature based on PHP is a great idea, unfortunately I think this is beyond my abilities to use vim script. If someone else found some time to finish this feature it would be greatly appreciated.

@JoyceBabu
Copy link

JoyceBabu commented Jan 23, 2020

@eknowlton I am also not too familiar with vim script. But I believe you can achieve it with the following code

if !exists('g:php_version_id')
        " Set the default global PHP version to 7.3
	let g:php_version_id = 70300
endif

if !exists('b:php_version_id' )
        " If a buffer level variable is not set, use the global version
	let b:php_version_id = g:php_version_id
endif

if b:php_version_id >= 70300
        " Enter the PHP 7.3+ specific modification here
endif

The above code allows setting a global and buffer level php version. The global PHP version can be set using g:php_version_id. The PHP version can be overridden on a workspace level using autocommand and the buffer level variable b:php_version_id.

As I mentioned above my knowledge of vimscript is very limited. I was able to turn on/off the flexible heredoc support by changing the variable g:php_version_id and b:php_version_id. But changing b:php_version_id affected all open buffers and not just the current buffer. Not sure if this is an issue with the script or my understanding of buffers and buffer variables.

Update: It was indeed an issue with my understanding of buffer. The code is working as intended.

@eknowlton
Copy link
Author

Thanks for the help @JoyceBabu, you're the best.
I'll see if I can make this happen now.

@JoyceBabu
Copy link

@eknowlton I have one more suggestion. You can remove most of the code duplication, by using a variable for the end pattern.

Since we cannot use the variable, directly, you will have to build a string with the syn region statement, and then execute it.

let s:heredoc_end = '^\z1\(;\=$\)\@='

if b:php_version_id >= 70300
	let s:heredoc_end = '^\s*\z1\>'
endif

exe 'syn region phpHereDoc matchgroup=Delimiter start="\(<<<\)\@3<=\z(\I\i*\)$" end="' . s:heredoc_end . '" contained contains=@Spell,phpIdentifier,phpIdentifierSimply,phpIdentifierComplex,phpSpecialChar,phpMethodsVar,phpStrEsc keepend extend'
exe 'syn region phpHereDoc matchgroup=Delimiter start=+\(<<<\)\@3<="\z(\I\i*\)"$+ end="' . s:heredoc_end . '" contained contains=@Spell,phpIdentifier,phpIdentifierSimply,phpIdentifierComplex,phpSpecialChar,phpMethodsVar,phpStrEsc keepend extend'

...

@StanAngeloff StanAngeloff merged commit 042e60a into StanAngeloff:master Feb 4, 2020
@StanAngeloff
Copy link
Owner

StanAngeloff commented Feb 4, 2020

Thank you all for participating in this pull request! I've now merged this to the master branch.

Thanks @eknowlton for contributing!
Thanks @JoyceBabu for helping push this forward with snippets and reviewing the code.
Thanks @Taluu for the initial suggestion RE: using PHP_VERSION_ID.

🎆

@mikehaertl
Copy link

@eknowlton @StanAngeloff Thanks for seeing some progress here.

One thing: There's a new config setting now, right? Could you also add this to the docs please?

@StanAngeloff
Copy link
Owner

@eknowlton @StanAngeloff Thanks for seeing some progress here.

One thing: There's a new config setting now, right? Could you also add this to the docs please?

I've added a commit to update the README.

I'm not sure what to do with the header in the Vim file itself, it's pretty out of date now.

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

Successfully merging this pull request may close these issues.

5 participants