Skip to content

Commit

Permalink
#1721 Replace codemirror-lang-r package with @codemirror/legacy-modes (
Browse files Browse the repository at this point in the history
…#1722)

Signed-off-by: Neha Gokhale <[email protected]>
Co-authored-by: Matt Howard <[email protected]>
  • Loading branch information
nmgokhale and matthoward366 authored Mar 4, 2024
1 parent 735d1f1 commit e761d9b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions canvas_modules/common-canvas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"@codemirror/lang-javascript": "^6.2.1",
"@codemirror/lang-python": "^6.1.4",
"@codemirror/lang-sql": "^6.5.5",
"codemirror-lang-r": "^0.1.0-2",
"@codemirror/language": "^6.10.1",
"@codemirror/legacy-modes": "^6.3.3",
"@codemirror/state": "^6.4.0",
"@codemirror/view": "^6.23.1",
"@codemirror/autocomplete": "^6.12.0",
Expand Down Expand Up @@ -119,7 +119,7 @@
},
"jest": {
"transformIgnorePatterns": [
"/node_modules/(?!(lezer-r|d3|d3-array|d3-axis|d3-brush|d3-chord|d3-color|d3-contour|d3-delaunay|d3-dispatch|d3-drag|d3-dsv|d3-ease|d3-fetch|d3-force|d3-format|d3-geo|d3-hierarchy|d3-interpolate|d3-path|d3-polygon|d3-quadtree|d3-random|d3-scale|d3-scale-chromatic|d3-selection|d3-shape|d3-time|d3-time-format|d3-timer|d3-transition|d3-zoom)/)"
"node_modules/(?!(@codemirror/legacy-modes|d3-*))"
],
"moduleFileExtensions": [
"js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,11 @@ import { Compartment } from "@codemirror/state";
import { tags } from "@lezer/highlight";
import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";
import { python } from "@codemirror/lang-python";
import { r } from "codemirror-lang-r";
import { sql } from "@codemirror/lang-sql";
import { javascript } from "@codemirror/lang-javascript";

import { getPythonHints } from "./languages/python-hint";
import { getRHints } from "./languages/r-hint";
import { rLanguage } from "./languages/r-hint";
import { clem } from "./languages/CLEM-hint";

const pxPerChar = 8.5;
Expand Down Expand Up @@ -168,14 +167,13 @@ class ExpressionControl extends React.Component {
this.origHint = getPythonHints();
break;
case "text/x-rsrc":
language = r();
this.origHint = getRHints();
language = rLanguage(); // custom language
break;
case "javascript":
language = javascript();
break;
default:
language = clem();
language = clem(); // custom language
}

// Custom completions add to the language completions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@
* limitations under the License.
*/

// Ref: https://cran.r-project.org/doc/manuals/r-release/R-lang.html#Reserved-words
// Search for "10.3.3 Reserved words"
const keywords = "if|else|repeat|while|function|for|in|next|break" +
"|TRUE|FALSE|NULL|Inf|NaN|NA|NA_integer_|NA_real_|NA_complex_|NA_character_";

const rKeywords = keywords.split("|");
import { LanguageSupport, StreamLanguage } from "@codemirror/language";
import { completeFromList } from "@codemirror/autocomplete";
import { r } from "@codemirror/legacy-modes/mode/r";

const builtIns = "zapsmall xzfile xtfrm xor writeLines writeChar writeBin write withVisible withRestarts within" +
" withCallingHandlers withAutoprint with while which weekdays warnings warning version Vectorize vector vapply" +
Expand Down Expand Up @@ -70,13 +67,23 @@ const rBuiltIns = builtIns.split(" ");
function getRHints() {
const rHints = [];

rKeywords.forEach((keyword) => rHints.push({ label: keyword, type: "keyword" }));

rBuiltIns.forEach((builtIn) => rHints.push({ label: builtIn, type: "keyword" }));

return rHints;
}

// Define R language
const rLan = StreamLanguage.define(r);

// Autocompletions
const rCompletion = rLan.data.of({
autocomplete: completeFromList(getRHints())
});

function rLanguage() {
return new LanguageSupport(rLan, [rCompletion]);
}

export {
getRHints
rLanguage
};
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,14 @@ describe("Test of Python and R expression controls", function() {
// test R autocomplete and syntax highlighting
cy.verifyTypeOfWordInExpressionEditor("# syntax testing", "comment", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("1", "number", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("text", "string", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("\n", "string", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("text\"", "string", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor('\n', "string", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("=", "operator", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("function", "keyword", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("Inf", "number", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("return", "keyword", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("Inf", "keyword", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("return", "variable", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor("<-", "operator", "conditionExpr");
cy.verifyTypeOfWordInExpressionEditor(";", "punctuation", "conditionExpr");

cy.enterTextInExpressionEditor("br", "conditionExpr");
cy.verifyNumberOfHintsInExpressionEditor(9);
Expand Down

0 comments on commit e761d9b

Please sign in to comment.