Skip to content
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

Upgrade to CodeMirror 6. #2595

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

drgrice1
Copy link
Member

@drgrice1 drgrice1 commented Oct 7, 2024

Some features of this change are improved syntax highlighting and the addition of autocompletion. To see available autocompletions at the current cursor location, you can type Ctrl-Space. In many cases, autocompletions are offered automatically when certain things have been typed.

CodeMirror 6 can not be used via script tags. So a PGProblemEditor package has been created for this. The repository for that is https://github.com/openwebwork/pg-problem-editor. An initial package has been published in npm at
https://www.npmjs.com/package/@openwebwork/pg-codemirror-editor. The real work is done in the codemirror-lang-pg package that adds support for the PG language in CodeMirror 6. The repository for that package is https://github.com/openwebwork/codemirror-lang-pg, and the npm package at https://www.npmjs.com/package/@openwebwork/codemirror-lang-pg.

The pg-problem-editor package is designed so that it can be used by webwork2, the standalone renderer, and possible others that want a PG problem editor. It may need a few tweaks to make it work for webwork3 in the future, but should also work for that.

The codemirror-lang-pg package consists of a base PG parser that is largely derived from the codemirror-lang-perl package (at https://github.com/drgrice1/codemirror-lang-perl and https://www.npmjs.com/package/codemirror-lang-perl) with a modifications specific to the perl available in a PG problem. The main differences are that its escape sequence is ~~, much of what can not be used in the safe compartment has been stripped out, and there are special blocks for PGML, PG text, and LaTeX image code. Then there are two other parsers. Those are the PGML and PG text parsers. Those parsers run on the contents of the PGML and PG text blocks found by the base PG parser. Note that the PGML parser is actually a javascript implementation of the parser in PGML.pl with some modifications to track position and things needed to associate the parsed tree with the original code.

Note that the configuration of theme, keymaps, and such are now part of the pg-codemirror-editor, and are shown in a panel of the editor instead of below as before.

Emacs and Vim keymaps are available, but at this point there is not a Sublime keymap for CodeMirror 6.

The themes that are available include a basic default theme (that is part of the pg-problem-editor package and just adds some syntax highlighting that is missing from the CodeMirror 6 default), the OndDark theme that is the only other theme provided by the CodeMirror 6 developers directly, the themes in the thememirror package, and the themes from https://github.com/craftzdog/cm6-themes which are in several separate npm packages. Note that both thememirror and craftzdog/cm6-themes provide a solarized light theme, and that is why there are two of those. They are slightly different though.

I am sure that there is a lot more work to do on this, and there are other things available with CodeMirror 6 that could probably be implemented. In any case, this is a good start.

@drgrice1 drgrice1 force-pushed the cm6-pg-editor branch 6 times, most recently from 7b456c9 to 20b242d Compare October 12, 2024 02:53
@somiaj
Copy link
Contributor

somiaj commented Oct 12, 2024

I was looking to add functions from the pg macros to the list of highlighted functions, such as PopUp, DropDown, etc. As I was creating a patch for this, it occurred to me, would it be possible to only highlight key words if the associated macro is loaded? Such as PopUp and DropDown will only be included if parserPopUp.pl keyword is found in the code? This might not be 100% accurate (such as if parserPopUp.pl is in a comment, though I guess we could just ignore comments for this case), or if the macros are loaded inside other macros (which I think is nonstandard for most additional things like parserPopUp.pl).

@drgrice1
Copy link
Member Author

No, you can't really do that in a reliable way. Generally CodeMirror 6 is designed to be non-contextual in its parsing.

@drgrice1 drgrice1 force-pushed the cm6-pg-editor branch 3 times, most recently from 97c81e9 to faa4fec Compare October 14, 2024 14:11
@drgrice1
Copy link
Member Author

I should point out that since the elements for selecting the theme, keymap, and such are part of the pg-codemirror-editor package, the strings used for those are not translatable unfortunately. That could be done by adding a translation method for that package, but would require translations for that package.

Some features of this change are improved syntax highlighting and
the addition of autocompletion.  To see available autocompletions at the
current cursor location, you can type Ctrl-Space.  In many cases,
autocompletions are offered automatically when certain things have been
typed.

CodeMirror 6 can not be used via script tags.  So a PGProblemEditor
package has been created for this.  The repository for that is
https://github.com/openwebwork/pg-problem-editor.  An initial package
has been published in npm at
https://www.npmjs.com/package/@openwebwork/pg-codemirror-editor. The
real work is done in the codemirror-lang-pg package that adds support
for the PG language in CodeMirror 6.  The repository for that package is
https://github.com/openwebwork/codemirror-lang-pg, and the npm package
at https://www.npmjs.com/package/@openwebwork/codemirror-lang-pg.

The pg-problem-editor package is designed so that it can be used by
webwork2, the standalone renderer, and possible others that want a PG
problem editor.  It may need a few tweaks to make it work for webwork3
in the future, but should also work for that.

The codemirror-lang-pg package consists of a base PG parser that is
largely derived from the codemirror-lang-perl package (at
https://github.com/drgrice1/codemirror-lang-perl and
https://www.npmjs.com/package/codemirror-lang-perl) with a modifications
specific to the perl available in a PG problem.  The main differences
are that its escape sequence is ~~, much of what can not be used in
the safe compartment has been stripped out, and there are special blocks
for PGML, PG text, and LaTeX image code.  Then there are two other
parsers.  Those are the PGML and PG text parsers.  Those parsers run on
the contents of the PGML and PG text blocks found by the base PG parser.
Note that the PGML parser is actually a javascript implementation of the
parser in PGML.pl with some modifications to track position and things
needed to associate the parsed tree with the original code.

Note that the configuration of theme, keymaps, and such are now part of
the pg-codemirror-editor, and are shown in a panel of the editor
instead of below as before.

Emacs and Vim keymaps are available, but at this point there is not a
Sublime keymap for CodeMirror 6.

The themes that are available include a basic default theme (that is
part of the pg-problem-editor package and just adds some syntax
highlighting that is missing from the CodeMirror 6 default), the OndDark
theme that is the only other theme provided by the CodeMirror 6
developers directly, the themes in the thememirror package, and the
themes from https://github.com/craftzdog/cm6-themes which are in several
separate npm packages.  Note that both thememirror and
craftzdog/cm6-themes provide a solarized light theme, and that is why
there are two of those.  They are slightly different though.

I am sure that there is a lot more work to do on this, and there are
other things available with CodeMirror 6 that could probably be
implemented.  In any case, this is a good start.
@drgrice1 drgrice1 force-pushed the cm6-pg-editor branch 2 times, most recently from ac27248 to 71afbdc Compare October 19, 2024 00:34
…ievement notifications.

The new codemirror-lang-mt package is used for this, and support for
that language has been added to the pg-codemirror-editor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants