Page MenuHomePhabricator

Add ability for a custom linter, specifically highlighting of characters or specific patterns of text
Closed, ResolvedPublic

Description

Requested at https://www.mediawiki.org/wiki/Help_talk:Extension:CodeMirror#Feedback_from_Wikisource_user :

I would also like to request a feature that might be useful for languages that use Cyrillic script. In Ukrainian alphabet there are a lot of letter that look identical to the Latin one (а & a; і & i; с & c etc.) and it is quite a hassle for Wikisource editors to deal with mix of these scripts in books. Would it be possible to have user set a list of characters that would be highlighted in the wikieditor? Currently, there are already some gadgets on Ukrainian Wikisource that do the highlighting of Latin characters, but they can only do that while viewing page, not during the editing.

After discussion, it sounds like this would be easiest to implement with a new JS entrypoint to add a custom linter. Then each wiki and individual users can create a gadget or user script specific to their needs.

Details

Event Timeline

Restricted Application added subscribers: Base, Aklapper. · View Herald Transcript

Personally I prefer the solution of providing a JS interface for gadgets. However, I think a cm prototype method + ext.CodeMirror.ready hook will be better than an additional hook.

Most users use scripts/gadgets for that sort of thing, see for example https://ru.wikipedia.org/wiki/Участник:DonRumata/Gadget-keybLayout.js
I think, since projects’ needs on this can be very different, this is better served by providing some access to extending CodeMirror in whichever way is easier. To clarify, it is specifically a common problem for wikis in Cyrillic that some users would (un)knowingly mix up the layouts in the same words, so just extending specialCharsExtension would probably not work as well for that use case since any hypothetical code would need to trigger on Пpимep (p/e are Latin) and not trigger on Example.

Since we have enabled linters in CodeMirror now, how about highlighting these characters as warnings? The interface will be much easier for us to provide and probably also more flexible for user script authors. For example, a user script can pass an arbitrary linter function ( content: string ) => Diagnostic[] | Promise<Diagnostic[]> to a prototype method cm.customLint( linter ), and here is the signature of interface Diagnostic. In this way, the user script can even suggest quick-fix actions.

Since we have enabled linters in CodeMirror now, how about highlighting these characters as warnings? The interface will be much easier for us to provide and probably also more flexible for user script authors. For example, a user script can pass an arbitrary linter function ( content: string ) => Diagnostic[] | Promise<Diagnostic[]> to a prototype method cm.customLint( linter ), and here is the signature of interface Diagnostic. In this way, the user script can even suggest quick-fix actions.

That sounds great! And I see from Discord we had another user who was content with a user script/gadget solution :)

In addition to adding the new public customLint() method, we could perhaps go ahead and implement one gadget for a select wiki, and publish that code in our documentation (say at Help:Extension:CodeMirror/Custom linters) so other wikis and individual users can borrow from it to craft their own implementation.

MusikAnimal renamed this task from Custom highlighting of characters, possibly with new CodeMirror preference to Add ability for a custom linter, specifically highlighting of characters or specific patterns of text.Mar 27 2026, 12:58 AM
MusikAnimal updated the task description. (Show Details)

And I see from Discord we had another user who was content with a user script/gadget solution :)

I think the request from Discord is related to bracket matching instead, for which I just submitted a patch.

You're right. Sorry, I misread!

My changes to the task description still apply though, I think – no need to bring preferences into this.

Change #1261901 had a related patch set uploaded (by Bhsd; author: Bhsd):

[mediawiki/extensions/CodeMirror@master] CodeMirror: applyLinter() method

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

Bhsd changed the task status from Open to In Progress.Mar 31 2026, 5:45 AM
Bhsd claimed this task.

Change #1261901 merged by jenkins-bot:

[mediawiki/extensions/CodeMirror@master] CodeMirror: applyLinter() method

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

The mentioned script can be converted to a CodeMirror extension like this (after the deployment next week):

const cyrPattern = '[\u0401-\u040F\u0410-\u042F\u0430-\u044F\u0451-\u045F\u0460-\u0486\u0490-\u04CC\u04D0-\u04F9]';
const latPattern = '[A-Za-zªºÀ-ÖØ-öø-ʸˠ-ˤᴀ-ᴥᴬ-ᵜᵢ-ᵥᵫ-ᵷᵹ-ᶾḀ-ỿⁱⁿₐ-ₜℲⅎⅠ-ↈⱠ-ⱿꜢ-ꞇꞋ-ꞿꟂ-ꟊꟵ-ꟿꬰ-ꭚꭜ-ꭤꭦ-ꭩff-stA-Za-z]';
const pattern = new RegExp( `${ latPattern }+(?=${ cyrPattern })|(${ cyrPattern }+)${ latPattern }+`, 'g' );
mw.hook( 'ext.CodeMirror.ready' ).add( ( cm ) => {
	cm.applyLinter( {
		pattern,
		callback( mt ) {
			return {
				from: mt.index + ( mt[ 1 ] || '').length,
				to: mt.index + mt[ 0 ].length,
				severity: 'warning',
				message: 'Latin letters among Cyrillic letters'
			};
		}
	} );
} );

we could perhaps go ahead and implement one gadget for a select wiki, and publish that code in our documentation (say at Help:Extension:CodeMirror/Custom linters) so other wikis and individual users can borrow from it to craft their own implementation.

An ongoing discussion on EN Wikisource that may be relevant: https://en.wikisource.org/wiki/Wikisource:Scriptorium#c-Nosferattus-20260406000700-Look-alike_Cyrillic_characters

In addition to adding the new public customLint() method, we could perhaps go ahead and implement one gadget for a select wiki, and publish that code in our documentation (say at Help:Extension:CodeMirror/Custom linters) so other wikis and individual users can borrow from it to craft their own implementation.

Hi! Do you still plan to proceed with an example gadget, or can I close this as the requested task has been completed?

Bhsd closed this task as Resolved.EditedSat, May 9, 1:50 AM
Bhsd moved this task from Feature requests to Done on the MediaWiki-extensions-CodeMirror board.

The task has been completed for one month, but gadget authors still need to implement the new JS interface for their specific needs.

Hi! Do you still plan to proceed with an example gadget, or can I close this as the requested task has been completed?

Sorry for the late reply! No, I don't plan to build a gadget unless requested. If/when that happens, I can link to it in the docs on mediawiki.org.