Code Monkey home page Code Monkey logo

Comments (6)

gshakhn avatar gshakhn commented on July 17, 2024

I recently started using StandardNotes and hit this issue for Scala. Some thoughts are below. Assuming the below works for you, I can put together a PR if you want.

The clike languages aren't working since the individual language definitions are based on the mime type, which isn't stored on the note as far as I can tell. One fix would be to store the CodeMirror name on the note instead of the mode. This would require changing the dropdown list to a list of names and storing it in a name attribute (or codeMirrorModeName to avoid confusion with the title of the note).

The fun part would be migrating existing notes. If the existing note has a codeMirrorModeName field, we're in the new state and can do CodeMirror.findModeByName. If the existing note has a mode field, we're in the old state. The CodeMirror API doesn't expose a findModeByMode method, so we'll have to filter CodeMirror.modeInfo ourselves. The latter is documented, so it should be safe to use.

Some questions:

  1. If the old mode is on the note, we could either save the new codeMirrorModeName immediately (and I'm guessing indirectly modify the modified time), or wait for the user to change the note before saving. Former is easier, but latter is probably better UX. Thoughts?

  2. The existing code tries to do findModeByExtension and findModeByMIME if it thinks the mode is an extension or mime type. With the current code, when would either of those be the case? Am I missing some context when either of those would be saved as the mode?

from code-editor.

moughxyz avatar moughxyz commented on July 17, 2024

Hey @gshakhn, thanks for looking into this. Is it possible to just create a different mode for each clike language? https://github.com/sn-extensions/code-editor/tree/master/vendor/modes/clike

Duplication is fine, if it means not having to tinker with the architecture too much. It looks like we have some if/else to handle the different mime types. Any idea if it would be possible to isolate each one into a separate mode file?

from code-editor.

gshakhn avatar gshakhn commented on July 17, 2024

I was able to hack something together by:

  1. Copying the clike directory and renaming it to scala.
  2. Renaming clike.js to scala.js.
  3. Changing line 49 of scala.js from CodeMirror.defineMode("clike",... to CodeMirror.defineMode("scala",...
  4. Changing line 489 of scala.js from:
  def("text/x-scala", {
    name: "clike",

to

  def("scala", {
    name: "scala",
  1. Changing {name: "Scala", mime: "text/x-scala", mode: "clike", ext: ["scala"]}, to {name: "Scala", mime: "text/x-scala", mode: "scala", ext: ["scala"]}, (I'm not sure if this was actually necessary. Did this early before tweaking the stuff below)

In theory it's possible. In practice, I wouldn't recommend this approach since this is fighting against how CodeMirror wants to work and will make upgrades more painful. Plus you'd be changing the mime type from text/x-scala to scala.

from code-editor.

gshakhn avatar gshakhn commented on July 17, 2024

Alternatively, you could also have a specific branch for each of the clike languages. e.g.

In changeMode:

      const actualMode = mode === 'scala' ? 'clike' : mode;
      const actualSpec = mode === 'scala' ? 'text/x-scala' : spec;
      editor.setOption("mode", actualSpec);
      CodeMirror.autoLoadMode(editor, actualMode);
      if (clientData) {
        clientData.mode = mode;
      }
      document.getElementById("select").selectedIndex = modes.indexOf(mode);

When a new clike language is added, you would have to add an extra branch in addition to adding it to the modes array. Given how often that happens, that's probably easier than my first suggested approach of migrating to storing the name.

from code-editor.

moughxyz avatar moughxyz commented on July 17, 2024

Interesting. I don't have the capacity for this now, so I'll either take a look in the future, or feel free to submit a PR :)

from code-editor.

gshakhn avatar gshakhn commented on July 17, 2024

Submitted #3

from code-editor.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.