This is an automated email from the ASF dual-hosted git repository.

heneveld pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brooklyn-ui.git


The following commit(s) were added to refs/heads/master by this push:
     new c7bae2d  Switch the linters to be asynchronous. Because CodeMirror can 
have only on type of linters, it's either sync or async. Better to switch to 
async to not restrict possible other linters defined downstream.
     new c93c14a  This closes #183
c7bae2d is described below

commit c7bae2d9cf25b4c788f2207195a3325f3a4ba9ad
Author: Thomas Bouron <thomas.bou...@cloudsoftcorp.com>
AuthorDate: Wed Jan 6 15:00:39 2021 +0000

    Switch the linters to be asynchronous. Because CodeMirror can have only on 
type of linters, it's either sync or async. Better to switch to async to not 
restrict possible other linters defined downstream.
---
 .../yaml-editor/addon/lint/lint-yaml-brooklyn.js   | 110 +++++++++++----------
 .../utils/yaml-editor/addon/lint/lint-yaml-tab.js  |  32 +++---
 ui-modules/utils/yaml-editor/yaml-editor.js        |  28 ++++--
 3 files changed, 91 insertions(+), 79 deletions(-)

diff --git a/ui-modules/utils/yaml-editor/addon/lint/lint-yaml-brooklyn.js 
b/ui-modules/utils/yaml-editor/addon/lint/lint-yaml-brooklyn.js
index e4cf85c..0c27c93 100644
--- a/ui-modules/utils/yaml-editor/addon/lint/lint-yaml-brooklyn.js
+++ b/ui-modules/utils/yaml-editor/addon/lint/lint-yaml-brooklyn.js
@@ -56,70 +56,72 @@ CodeMirror.registerGlobalHelper('lint', 'yamlBrooklyn', 
(mode, cm) => (mode.name
     (text, options, cm) => lint(rootValidator, rootSchema, text, options, cm));
 
 function lint(validator, baseSchema, text, options, cm) {
-    let issues = [];
-    let parser = new JsYamlParser();
+    return new Promise((resolve, reject) =>  {
+        let issues = [];
+        let parser = new JsYamlParser();
 
-    try {
-        let root = parser.parse(text);
+        try {
+            let root = parser.parse(text);
 
-        if (root) {
-            validator.validate(root.result, JSON.parse(baseSchema), 
{propertyName: 'blueprint', nestedErrors: true}).errors.forEach(error => {
-                let from = CodeMirror.Pos(0, 0);
-                let to = CodeMirror.Pos(0, 0);
-                let yamlNode = findYamlNode(root, error.instance);
+            if (root) {
+                validator.validate(root.result, JSON.parse(baseSchema), 
{propertyName: 'blueprint', nestedErrors: true}).errors.forEach(error => {
+                    let from = CodeMirror.Pos(0, 0);
+                    let to = CodeMirror.Pos(0, 0);
+                    let yamlNode = findYamlNode(root, error.instance);
 
-                if (yamlNode) {
-                    switch (error.name) {
-                        case 'anyOf':
-                        case 'oneOf':
-                        case 'allOf':
-                        case 'not':
-                        case 'required':
-                            from = to = getPostFromIndex(yamlNode.start, cm);
-                            break;
-                        case 'additionalProperties':
-                            let childNode = yamlNode.children.find(child => 
child.result === error.argument);
-                            from = getPostFromIndex(childNode.start, cm);
-                            to = getPostFromIndex(childNode.end, cm);
-                            break;
-                        default:
-                            from = getPostFromIndex(yamlNode.start, cm);
-                            to = getPostFromIndex(yamlNode.end, cm);
-                            break;
+                    if (yamlNode) {
+                        switch (error.name) {
+                            case 'anyOf':
+                            case 'oneOf':
+                            case 'allOf':
+                            case 'not':
+                            case 'required':
+                                from = to = getPostFromIndex(yamlNode.start, 
cm);
+                                break;
+                            case 'additionalProperties':
+                                let childNode = yamlNode.children.find(child 
=> child.result === error.argument);
+                                from = getPostFromIndex(childNode.start, cm);
+                                to = getPostFromIndex(childNode.end, cm);
+                                break;
+                            default:
+                                from = getPostFromIndex(yamlNode.start, cm);
+                                to = getPostFromIndex(yamlNode.end, cm);
+                                break;
+                        }
                     }
+                    issues.push({
+                        from: from,
+                        to: to,
+                        message: error.stack
+                    });
+                });
+            }
+        } catch (err) {
+            if (err instanceof YAMLException) {
+                // Inspire by the Mark.getSnippet() code: 
https://github.com/nodeca/js-yaml/blob/master/lib/js-yaml/mark.js#L16-L52
+                let start = err.mark.position;
+                while (start > 0 && 
'\x00\r\n\x85\u2028\u2029'.indexOf(err.mark.buffer.charAt(start - 1)) === -1) {
+                    start--;
                 }
+                let end = err.mark.position - start;
+                let errorText = err.mark.buffer.substr(start, end);
+
                 issues.push({
-                    from: from,
-                    to: to,
-                    message: error.stack
+                    from: CodeMirror.Pos(err.mark.line, 
cm.getLine(err.mark.line-1).indexOf(errorText)),
+                    to: CodeMirror.Pos(err.mark.line, 
cm.getLine(err.mark.line-1).indexOf(errorText) + (errorText.length > 0 ? 
errorText.length : 1)),
+                    message: err.reason
+                });
+            } else {
+                issues.push({
+                    from: CodeMirror.Pos(0, 0),
+                    to: CodeMirror.Pos(0, 0),
+                    message: err.message
                 });
-            });
-        }
-    } catch (err) {
-        if (err instanceof YAMLException) {
-            // Inspire by the Mark.getSnippet() code: 
https://github.com/nodeca/js-yaml/blob/master/lib/js-yaml/mark.js#L16-L52
-            let start = err.mark.position;
-            while (start > 0 && 
'\x00\r\n\x85\u2028\u2029'.indexOf(err.mark.buffer.charAt(start - 1)) === -1) {
-                start--;
             }
-            let end = err.mark.position - start;
-            let errorText = err.mark.buffer.substr(start, end);
-
-            issues.push({
-                from: CodeMirror.Pos(err.mark.line, 
cm.getLine(err.mark.line).indexOf(errorText)),
-                to: CodeMirror.Pos(err.mark.line, 
cm.getLine(err.mark.line).indexOf(errorText) + (errorText.length > 0 ? 
errorText.length : 1)),
-                message: err.reason
-            });
-        } else {
-            issues.push({
-                from: CodeMirror.Pos(0, 0),
-                to: CodeMirror.Pos(0, 0),
-                message: err.message
-            });
         }
-    }
 
-    return issues;
+        return resolve(issues);
+    });
 }
 
 function findYamlNode(node, search) {
diff --git a/ui-modules/utils/yaml-editor/addon/lint/lint-yaml-tab.js 
b/ui-modules/utils/yaml-editor/addon/lint/lint-yaml-tab.js
index 3485ea3..3cd6090 100644
--- a/ui-modules/utils/yaml-editor/addon/lint/lint-yaml-tab.js
+++ b/ui-modules/utils/yaml-editor/addon/lint/lint-yaml-tab.js
@@ -19,22 +19,24 @@
 const CodeMirror = require('codemirror');
 
 CodeMirror.registerGlobalHelper('lint', 'yaml-tab', mode => mode.name === 
'yaml', (text, options, cm) => {
-    let issues = [];
+    return new Promise(resolve => {
+        let issues = [];
 
-    for (let index = 0; index < cm.lineCount(); index++) {
-        cm.getLine(index)
-            .split('')
-            .map((c, i) => ({c: c, i: i}))
-            .filter(item => item.c === '\t')
-            .forEach(item => {
-                issues.push({
-                    from: CodeMirror.Pos(index, item.i),
-                    to: CodeMirror.Pos(index, item.i + 1),
-                    message: 'Tab character detected. We strongly recommend 
you to use spaces instead to avoid indentation issues',
-                    severity: 'warning'
+        for (let index = 0; index < cm.lineCount(); index++) {
+            cm.getLine(index)
+                .split('')
+                .map((c, i) => ({c: c, i: i}))
+                .filter(item => item.c === '\t')
+                .forEach(item => {
+                    issues.push({
+                        from: CodeMirror.Pos(index, item.i),
+                        to: CodeMirror.Pos(index, item.i + 1),
+                        message: 'Tab character detected. We strongly 
recommend you to use spaces instead to avoid indentation issues',
+                        severity: 'warning'
+                    });
                 });
-            });
-    }
+        }
 
-    return issues;
+        resolve(issues);
+    });
 });
\ No newline at end of file
diff --git a/ui-modules/utils/yaml-editor/yaml-editor.js 
b/ui-modules/utils/yaml-editor/yaml-editor.js
index a9c1b57..a079e6e 100644
--- a/ui-modules/utils/yaml-editor/yaml-editor.js
+++ b/ui-modules/utils/yaml-editor/yaml-editor.js
@@ -59,13 +59,17 @@ export function yamlEditorDirective($rootScope, brSnackbar) 
{
             type: $scope.type
         });
 
-        let lint = (text, options, cm) => {
-            let issues = cm.getHelpers(CodeMirror.Pos(0, 0), 
'lint').reduce((issues, helper) => issues.concat(helper(text, options, cm)), 
[]);
-
-            $rootScope.$broadcast('yaml.lint', issues.some(issue => 
!issue.severity || issue.severity === 'error'), cm.getValue());
-
-            return issues;
-        };
+        let lint = (text, cb, options, cm) => {
+            let promises = cm.getHelpers(CodeMirror.Pos(0, 0), 
'lint').reduce((issues, helper) => issues.concat(helper(text, options, cm)), 
[]);
+
+            Promise.all(promises).then(issues => {
+                $rootScope.$broadcast('yaml.lint', issues.some(issue => 
!issue.severity || issue.severity === 'error'), cm.getValue());
+                cb(cm, issues.reduce((acc, arr) => {
+                    acc.push(...arr);
+                    return acc;
+                }, []));
+            });
+        }
         let hint = (cm, callback, options) => {
             let loadingState = createLoadingState();
             let removeLoadingState = () => {
@@ -127,7 +131,11 @@ export function yamlEditorDirective($rootScope, 
brSnackbar) {
                 name: 'yaml',
                 globalVars: true
             },
-            lint: lint,
+            lint: {
+                getAnnotations: lint,
+                async: true,
+                timeout: 1000
+            },
             extraKeys: {
                 'Ctrl-Space': 'autocomplete',
                 'Tab': cm => {
@@ -178,9 +186,9 @@ export function yamlEditorDirective($rootScope, brSnackbar) 
{
             });
         });
 
-        $scope.$on(`${MODULE_NAME}.cm`, callback => {
+        $scope.$on(`${MODULE_NAME}.cm`, (event, callback) => {
             if (angular.isFunction(callback)) {
-                callback.apply(null, [$scope.cm]);
+                callback.apply(null, [CodeMirror, $scope.cm]);
             }
         });
     }

Reply via email to