Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 469249

Summary: Make grammar provider configurable in edit.js
Product: [ECD] Orion Reporter: Miro Spönemann <miro.spoenemann>
Component: EditorAssignee: Grant Gayed <grant_gayed>
Status: RESOLVED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: mamacdon
Version: 8.0   
Target Milestone: 10.0   
Hardware: PC   
OS: Mac OS X   
See Also: https://git.eclipse.org/r/50522
Whiteboard:

Description Miro Spönemann CLA 2015-06-03 05:50:39 EDT
Currently edit.js loads the syntax file with

var folderName = contentType.replace(/[*|:/".<>?+]/g, '_');
require(["./stylers/" + folderName + "/syntax"] ...);

This should be made configurable with the passed options, so other paths can be set for custom languages.
Comment 1 Eclipse Genie CLA 2015-06-19 08:58:24 EDT
New Gerrit change created: https://git.eclipse.org/r/50522
Comment 2 Mark Macdonald CLA 2015-06-22 14:18:24 EDT
It might make sense to generalize this a bit, and allow the caller to provide an alternative impl of lines 262-275 [1]

For example, suppose the caller could provide a function like this:

> function loadGrammar(contentType, onDone, onError) {
>    // Obtains the grammar for the given contentType.
>    // On success invokes onDone(grammar)
>    // On error   invokes onError(error)
> }

This way, the caller injects the loading logic into the editor. So they are not forced to provide a require()-able path; they can obtain a grammar through any means.

@Grant what is your opinion on this?

[1] https://github.com/eclipse/orion.client/blob/master/bundles/org.eclipse.orion.client.editor/web/orion/editor/edit.js#L262-L275
Comment 3 Grant Gayed CLA 2015-06-22 14:32:09 EDT
(In reply to Mark Macdonald from comment #2)

Agreed, this would definitely be a better path to take.
Comment 4 Miro Spönemann CLA 2015-06-25 06:01:40 EDT
I think both would make sense as alternative options:
  options.grammarLoader to pass a function that obtains the syntax
  options.grammarModule to pass a module name to be loaded with require
Comment 5 Grant Gayed CLA 2015-06-25 22:18:40 EDT
Having both seems redundant, in the interest of API succinctness I've only added the option to provide a function.  The function must return a promise so that if needed it can answer the grammars asynchronously.  The commit is: http://git.eclipse.org/c/orion/org.eclipse.orion.client.git/commit/?id=b847251179cd41a62222443a7c3f84efcefcff55 .

An example of using it follows, based on org.eclipse.orion.client.editor/web/examples/editor/edit.html.  As a side note, in the new stand-alone editor story this would be done by simply providing the additional language grammars in new plug-in(s).

<script>
    /*eslint-env browser, amd*/
    require({
        baseUrl: '../..',
        paths: {
            i18n: 'requirejs/i18n',
            csslint: 'csslint/csslint',
        }
    });
    var CONTENT_TYPE = "application/asdf";
    require(["orion/editor/edit", "orion/Deferred"], function(edit, Deferred) {
        edit({
            contentType: CONTENT_TYPE,
            grammarProvider: function(contentType) {
                var result = new Deferred();
                if (contentType === CONTENT_TYPE) {					        require(["orion/editor/stylers/application_javascript/syntax"],
                        function(grammar) {
                            result.resolve(grammar);
                        },
                        function(error) {
                            result.reject();
                        });
                } else {
                    result.resolve();
                }
                return result;
            }
        });
    });
</script>