Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Merge pull request #58 from WordPress/fix/mixed-mode-autocomplete

Improve mixed-mode autocomplete hints, including PHP
  • Loading branch information...
westonruter committed Aug 28, 2017
2 parents f483af7 + 0f0643e commit 124dd695a3c5f3bb0fabd1f33bbc3ef51353f04a
Showing with 30 additions and 14 deletions.
  1. +23 −9 wp-admin/js/code-editor.js
  2. +7 −5 wp-includes/general-template-addendum.php
@@ -99,22 +99,36 @@ if ( 'undefined' === typeof window.wp.codeEditor ) {
wp.codeEditor.instances.push( editor );

if ( editor.showHint ) {
editor.on( 'keyup', function( _editor, event ) {
var shouldAutocomplete, isAlphaKey = /^[a-zA-Z]$/.test( event.key );
editor.on( 'keyup', function( _editor, event ) { // eslint-disable-line complexity
var shouldAutocomplete, isAlphaKey = /^[a-zA-Z]$/.test( event.key ), lineBeforeCursor, innerMode, token;
if ( editor.state.completionActive && isAlphaKey ) {
return;
}
shouldAutocomplete = isAlphaKey;
if ( ! shouldAutocomplete && 'htmlmixed' === instanceSettings.codemirror.mode ) {

// Prevent autocompletion in string literals or comments.
token = editor.getTokenAt( editor.getCursor() );
if ( 'string' === token.type || 'comment' === token.type ) {
return;
}

innerMode = CodeMirror.innerMode( editor.getMode(), token.state ).mode.name;
lineBeforeCursor = editor.doc.getLine( editor.doc.getCursor().line ).substr( 0, editor.doc.getCursor().ch );
if ( 'html' === innerMode || 'xml' === innerMode ) {
shouldAutocomplete =
'<' === event.key ||
'/' === event.key && /<\/$/.test( editor.doc.getLine( editor.doc.getCursor().line ).substr( 0, editor.doc.getCursor().ch ) );
} else if ( ! shouldAutocomplete && 'text/css' === instanceSettings.codemirror.mode ) {
'/' === event.key && 'tag' === token.type ||
isAlphaKey && 'tag' === token.type ||
isAlphaKey && 'attribute' === token.type ||
'=' === token.string && token.state.htmlState && token.state.htmlState.tagName;
} else if ( 'css' === innerMode ) {
shouldAutocomplete =
isAlphaKey ||
':' === event.key ||
' ' === event.key && /:\s+$/.test( editor.doc.getLine( editor.doc.getCursor().line ).substr( 0, editor.doc.getCursor().ch ) );
} else if ( ! shouldAutocomplete && 'text/javascript' === instanceSettings.codemirror.mode ) {
shouldAutocomplete = '.' === event.key;
' ' === event.key && /:\s+$/.test( lineBeforeCursor );
} else if ( 'javascript' === innerMode ) {
shouldAutocomplete = isAlphaKey || '.' === event.key;
} else if ( 'clike' === innerMode && 'application/x-httpd-php' === editor.options.mode ) {
shouldAutocomplete = 'keyword' === token.type || 'variable' === token.type;
}
if ( shouldAutocomplete ) {
CodeMirror.commands.autocomplete( editor, null, { completeSingle: false } );
@@ -232,14 +232,15 @@ function wp_enqueue_code_editor( $settings ) {
if ( isset( $settings['codemirror']['mode'] ) ) {
switch ( $settings['codemirror']['mode'] ) {
case 'application/x-httpd-php':
wp_enqueue_script( 'codemirror-mode-html' );
wp_enqueue_script( 'codemirror-mode-php' );
wp_enqueue_script( 'codemirror-mode-javascript' );
wp_enqueue_script( 'codemirror-mode-css' );
break;
wp_enqueue_script( 'codemirror-addon-hint-show' );
/* falls through */
case 'htmlmixed':
wp_enqueue_script( 'codemirror-mode-html' );
wp_enqueue_script( 'codemirror-addon-hint-html' );
wp_enqueue_script( 'codemirror-addon-hint-javascript' );
wp_enqueue_script( 'codemirror-addon-hint-css' );
wp_enqueue_style( 'codemirror-addon-show-hint' );
if ( ! empty( $settings['codemirror']['lint'] ) ) {
if ( ! current_user_can( 'unfiltered_html' ) ) {
@@ -251,6 +252,7 @@ function wp_enqueue_code_editor( $settings ) {
case 'text/javascript':
wp_enqueue_script( 'codemirror-mode-javascript' );
wp_enqueue_script( 'codemirror-addon-hint-javascript' );
wp_enqueue_style( 'codemirror-addon-show-hint' );
if ( ! empty( $settings['codemirror']['lint'] ) ) {
wp_enqueue_script( 'codemirror-addon-lint-javascript' );
@@ -262,6 +264,7 @@ function wp_enqueue_code_editor( $settings ) {
case 'text/css':
wp_enqueue_script( 'codemirror-mode-css' );
wp_enqueue_script( 'codemirror-addon-hint-css' );
wp_enqueue_style( 'codemirror-addon-show-hint' );
if ( ! empty( $settings['codemirror']['lint'] ) ) {
wp_enqueue_script( 'codemirror-addon-lint-css' );
@@ -270,7 +273,6 @@ function wp_enqueue_code_editor( $settings ) {
}
if ( ! empty( $settings['codemirror']['lint'] ) ) {
wp_enqueue_style( 'codemirror-addon-show-hint' );
wp_enqueue_style( 'codemirror-addon-lint' );
}
}

0 comments on commit 124dd69

Please sign in to comment.
You can’t perform that action at this time.