-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Also apply fixes for PHP autocompletion #10807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Uses the work from @siliconforks, and adds a fix for PHP editors.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
src/js/_enqueues/wp/code-editor.js
Outdated
| isAlphaKey && 'attribute' === token.type || | ||
| '=' === token.string && token.state.htmlState && token.state.htmlState.tagName; | ||
| '=' === event.key && | ||
| '=' === token.string && token.state.curState && token.state.curState.htmlState && token.state.curState.htmlState.tagName; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to log out token.state.curState && token.state.curState.htmlState && token.state.curState.htmlState.tagName and it's always undefined. So this seems to have the effect of disabling the condition entirely.
src/js/_enqueues/wp/code-editor.js
Outdated
| /* jshint esversion: 11 */ | ||
| /* eslint-env es2020 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm opting-in this file for ES11/2020 to be able to use optional chaining operator. This is, in fact, being used in the core codebase elsewhere, specifically in built files coming from Gutenberg: https://github.com/search?q=repo%3AWordPress%2FWordPress+%2F%5Cw%2B%5C%3F%5C.%5Cw%2B%2F+language%3AJavaScript&type=code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See also Core-64562
src/js/_enqueues/wp/code-editor.js
Outdated
| '/' === event.key && 'tag' === token.type || | ||
| isAlphaKey && 'tag' === token.type || | ||
| isAlphaKey && 'attribute' === token.type || | ||
| '=' === token.string && token.state.htmlState && token.state.htmlState.tagName; | ||
| ( '/' === event.key && 'tag' === token.type ) || | ||
| ( isAlphaKey && 'tag' === token.type ) || | ||
| ( isAlphaKey && 'attribute' === token.type ) || | ||
| ( '=' === event.key && '=' === token.string && token.state.curState?.htmlState?.tagName ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added parentheses because the existing logic was confusing to read. I have no one to blame but myself, as this originated in the Better Code Editing feature plugin: WordPress/better-code-editing@f49a012
|
Just to summarize the situation ... there are two (actually three) separate fixes required here:
@westonruter - are you testing with the old version of CodeMirror (the one that shipped with WordPress 6.9) or the newer version of CodeMirror (the one you're working on upgrading to in #48456)? |
@siliconforks I'm testing with the latest CodeMirror v5 as Core-48456 was committed (438132f) and is part of this PR's commit history. |
|
For reference, in the latest CodeMirror v5, here's what a {
"start": 11,
"end": 12,
"string": "=",
"type": null,
"state": {
"inTag": null,
"localMode": null,
"htmlState": {
"indented": 0,
"tagName": "input",
"tagStart": 0,
"context": null
}
}
} |
|
On further investigation, it looks like I was wrong about CodeMirror changing the token structure in different versions - it's not actually a version issue, it's an issue with different modes:
So, the it seems that the code needs to handle both cases. This should work: shouldAutocomplete = (
'<' === event.key ||
( '/' === event.key && 'tag' === token.type ) ||
( isAlphaKey && 'tag' === token.type ) ||
( isAlphaKey && 'attribute' === token.type ) ||
/*
* In .html files the tag name is in token.state.htmlState.tagName;
* in .php files the tag name is in token.state.curState.htmlState.tagName.
*/
( '=' === event.key && '=' === token.string && ( token.state.htmlState?.tagName || token.state.curState?.htmlState?.tagName ) )
); |
|
@siliconforks And what purpose does |
Yes, I'm assuming the original code ( |
Co-authored-by: siliconforks <[email protected]>
…into codemirror-42822
OK, I applied that change in 8cd4589. Please test. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR applies autocompletion fixes for the WordPress code editor, specifically addressing issues with PHP autocompletion logic and improving condition clarity across HTML, CSS, and JavaScript modes.
Changes:
- Added ES2020 environment configuration for eslint
- Fixed PHP autocompletion to only trigger on alpha keys when token type is keyword or variable
- Improved condition clarity by adding parentheses around compound boolean expressions
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/js/_enqueues/wp/code-editor.js
Outdated
| ( '/' === event.key && 'tag' === token.type ) || | ||
| ( isAlphaKey && 'tag' === token.type ) || | ||
| ( isAlphaKey && 'attribute' === token.type ) || | ||
| ( '=' === event.key && '=' === token.string && ( |
Copilot
AI
Feb 2, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition checks both event.key and token.string for '=', which may be redundant or indicate a logical error. If event.key is '=' and token.string is also '=', this suggests the same data is being checked twice. Verify if both checks are necessary or if one should be checking a different property.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@siliconforks right, is '=' === event.key not redundant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or maybe '=' === token.string is actually redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Certainly the event.key check is not redundant here - it is necessary to check event.key to make sure the user didn't just press one of the arrow keys.
Possibly the token.string check is redundant now that event.key is being checked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does seem redundant. This is the token is the token immediately preceding the cursor, so it will always be = in this case if event.key is =. It works well without it:
Screen.Recording.2026-02-02.at.11.27.26.mov
Removed in b5997a8
…tribute values and PHP keywords/variables. Developed in #10807 Follow-up to [41376]. Props siliconforks, joedolson, westonruter, afercia, jorbin. Fixes #42822. git-svn-id: https://develop.svn.wordpress.org/trunk@61579 602fd350-edb4-49c9-b593-d223f7449a82
…tribute values and PHP keywords/variables. Developed in WordPress/wordpress-develop#10807 Follow-up to [41376]. Props siliconforks, joedolson, westonruter, afercia, jorbin. Fixes #42822. Built from https://develop.svn.wordpress.org/trunk@61579 git-svn-id: http://core.svn.wordpress.org/trunk@60890 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Uses the work from @siliconforks, and adds a fix for PHP editors.
Trac ticket: https://core.trac.wordpress.org/ticket/42822
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.