Page MenuHomePhabricator

Add CodeMirror integration to 2017 editor + DiscussionTools
Closed, ResolvedPublic

Description

Problem

CodeMirror would be especially useful in DiscussionTools, since templates can't be used in the visual mode of the reply tool, forcing users to write complicated syntax without the assistance of syntax highlighting.

Related: We could in theory add CodeMirror to any and all VE instances. That would be more ideal so users of the 2017 editor can enjoy the benefits of CodeMirror everywhere and not just when editing desktop articles. However let's start with just DiscussionTools :)

Technical background

When CodeMirror integration with the 2017 editor was added for CodeMirror 5 (aka CM5), we did so using the $wgVisualEditorPluginModules extension attribute. This worked fine until the CodeMirror 6 project started, when we needed to programmatically load a specific module based on whether we're using CM5 or CM6. That led to race conditions which were fixed in T374072. In the process, we incidentally hard-wired CodeMirror to only ever load for article targets on desktop.

Beyond simply registering the plugin at the correct time, there are some visual tweaks needed to make CodeMirror look nice on non-article targets like DiscussionTools. This can be solved with generalizing the CSS beyond what we have now. The older CSS was more or less written solely for desktop articles as that was the only place CodeMirror was available.

Solution
  • We need to load the plugin differently for CM5 and CM6. CM5 is deprecated so it can remain only for desktop article targets using the ve.loadModules, as per T374072. CM6 meanwhile can be loaded using mw.libs.ve.targetLoader.addPlugin() which is the advertised means to register a plugin for all targets.
  • The CSS needs to be changed to apply to all targets, and ideally without any need for skin-specific overrides. The one thing that differs is the padding around the VE surface, and we can compute that at run time instead of guessing in our CSS.
  • Users should be able to easily disable CodeMirror if they choose to do so. On desktop, the existing icon should be in the toolbar (to later be replaced with the new icon, see T174145). On mobile, the button can follow the default overflow behaviour, and for DiscussionTools, it can appear in the "Style text" menu.
  • Line numbering should be disabled in DiscussionTools. Line numbering isn't as useful in discussions, and consumes too much space on smaller viewports.

Screenshots:

Screenshot from 2025-10-21 16-42-40.png (704×1 px, 83 KB)
Screenshot from 2025-10-21 16-59-41.png (592×608 px, 66 KB)
Screenshot from 2025-10-21 17-07-59.png (808×616 px, 76 KB)

Event Timeline

I've tagged DiscussionTools for visibility, though no code changes are needed there.

CodeMirror would be especially useful in DiscussionTools, since templates can't be used in the visual mode, forcing users to write complicated syntax without the assistance of syntax highlighting.

Actually, templates can be used in the visual mode of the new topic tool (by typing {{; there is no toolbar button). However, they are indeed disabled in the visual mode of the reply tool because of issues with multi-line templates (T253667).

Change #1197727 had a related patch set uploaded (by MusikAnimal; author: MusikAnimal):

[mediawiki/extensions/CodeMirror@master] visualEditor: load CM6 on all targets (i.e. DiscussionTools) and mobile

https://gerrit.wikimedia.org/r/1197727

MusikAnimal renamed this task from Load CodeMirror with the 2017 editor for all applicable targets (i.e. DiscussionTools) to Add CodeMirror integration to 2017 editor + DiscussionTools.Oct 22 2025, 12:57 AM
MusikAnimal updated the task description. (Show Details)

@Bhsd alerted me that I actually had some forgotten uncommitted code in my local checkout of DiscussionTools that made my vision work… :facepalm:

I think the "make it work everywhere" approach is still doable, but we should start with the primary motivation of this task, which is DiscussionTools.

Tech News worthy, I think! Adding the tag so we don't forget.

@MusikAnimal we can start working on a draft for Tech News until it's ready to be published. Can you suggest the best wording please?

I can add something to Tech News once r1197727 is merged. If it's not merged by the time translations are frozen for Tech/News/2025/45, I figure we should wait until next week. Ideally the notice goes out the same week as the change.

Maybe I should have waited to add the user-notice tag to this task. I just didn't want to forget! :)

@MusikAnimal no worries about the tag, we usually seek clarification first.

It's a new week, if the merge has been done, you can add the entry to this week's draft. Thank you!

Change #1197727 merged by jenkins-bot:

[mediawiki/extensions/CodeMirror@master] CodeMirrorVisualEditor: add integration with DiscussionTools

https://gerrit.wikimedia.org/r/1197727

Apologies for the delay! With the patch merged, I have added an entry the upcoming Tech News.

Bhsd moved this task from Improvement to Done on the MediaWiki-extensions-CodeMirror board.