Added syntax highlighting to GQL Queries
This commit is contained in:
@@ -22,10 +22,13 @@
|
|||||||
const DEFAULT_THEME = "twilight"
|
const DEFAULT_THEME = "twilight"
|
||||||
|
|
||||||
import ace from "ace-builds"
|
import ace from "ace-builds"
|
||||||
import * as gql from "graphql"
|
|
||||||
import { getAutocompleteSuggestions } from "graphql-language-service-interface"
|
|
||||||
import "ace-builds/webpack-resolver"
|
import "ace-builds/webpack-resolver"
|
||||||
import "ace-builds/src-noconflict/ext-language_tools"
|
import "ace-builds/src-noconflict/ext-language_tools"
|
||||||
|
import "ace-builds/src-noconflict/mode-graphqlschema"
|
||||||
|
import { defineGQLLanguageMode } from "~/functions/syntax/gqlQueryLangMode"
|
||||||
|
|
||||||
|
import * as gql from "graphql"
|
||||||
|
import { getAutocompleteSuggestions } from "graphql-language-service-interface"
|
||||||
import debounce from "../../functions/utils/debounce"
|
import debounce from "../../functions/utils/debounce"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@@ -40,7 +43,7 @@ export default {
|
|||||||
},
|
},
|
||||||
lang: {
|
lang: {
|
||||||
type: String,
|
type: String,
|
||||||
default: "json",
|
default: "gql-query",
|
||||||
},
|
},
|
||||||
onRunGQLQuery: {
|
onRunGQLQuery: {
|
||||||
type: Function,
|
type: Function,
|
||||||
@@ -85,6 +88,8 @@ export default {
|
|||||||
},
|
},
|
||||||
|
|
||||||
mounted() {
|
mounted() {
|
||||||
|
defineGQLLanguageMode(ace)
|
||||||
|
|
||||||
let langTools = ace.require("ace/ext/language_tools")
|
let langTools = ace.require("ace/ext/language_tools")
|
||||||
|
|
||||||
const editor = ace.edit(this.$refs.editor, {
|
const editor = ace.edit(this.$refs.editor, {
|
||||||
@@ -190,7 +195,7 @@ export default {
|
|||||||
this.parseContents(this.cacheValue)
|
this.parseContents(this.cacheValue)
|
||||||
},
|
},
|
||||||
|
|
||||||
parseContents: debounce(function(content) {
|
parseContents: debounce(function (content) {
|
||||||
if (content !== "") {
|
if (content !== "") {
|
||||||
try {
|
try {
|
||||||
const doc = gql.parse(content)
|
const doc = gql.parse(content)
|
||||||
|
|||||||
115
functions/syntax/gqlQueryLangMode.js
Normal file
115
functions/syntax/gqlQueryLangMode.js
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
export function defineGQLLanguageMode(ace) {
|
||||||
|
// Highlighting
|
||||||
|
ace.define(
|
||||||
|
"ace/mode/gql-query-highlight",
|
||||||
|
["require", "exports", "ace/lib/oop", "ace/mode/text_highlight_rules"],
|
||||||
|
(aceRequire, exports) => {
|
||||||
|
const oop = aceRequire("ace/lib/oop")
|
||||||
|
|
||||||
|
const TextHighlightRules = aceRequire("ace/mode/text_highlight_rules").TextHighlightRules
|
||||||
|
|
||||||
|
const GQLQueryTextHighlightRules = function () {
|
||||||
|
var keywords =
|
||||||
|
"type|interface|union|enum|schema|input|implements|extends|scalar|fragment|query|mutation|subscription"
|
||||||
|
|
||||||
|
var dataTypes = "Int|Float|String|ID|Boolean"
|
||||||
|
|
||||||
|
var literalValues = "true|false|null"
|
||||||
|
|
||||||
|
var escapeRe = /\\(?:u[\da-fA-f]{4}|.)/
|
||||||
|
|
||||||
|
var keywordMapper = this.createKeywordMapper(
|
||||||
|
{
|
||||||
|
keyword: keywords,
|
||||||
|
"storage.type": dataTypes,
|
||||||
|
"constant.language": literalValues,
|
||||||
|
},
|
||||||
|
"identifier"
|
||||||
|
)
|
||||||
|
|
||||||
|
this.$rules = {
|
||||||
|
start: [
|
||||||
|
{
|
||||||
|
token: "comment",
|
||||||
|
regex: "#.*$",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
token: "paren.lparen",
|
||||||
|
regex: /[\[({]/,
|
||||||
|
next: "start",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
token: "paren.rparen",
|
||||||
|
regex: /[\])}]/,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
token: keywordMapper,
|
||||||
|
regex: "[a-zA-Z_][a-zA-Z0-9_$]*\\b",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
token: "string", // character
|
||||||
|
regex: "'(?:" + escapeRe + "|.)?'",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
token: "string.start",
|
||||||
|
regex: '"',
|
||||||
|
stateName: "qqstring",
|
||||||
|
next: [
|
||||||
|
{ token: "string", regex: /\\\s*$/, next: "qqstring" },
|
||||||
|
{ token: "constant.language.escape", regex: escapeRe },
|
||||||
|
{ token: "string.end", regex: '"|$', next: "start" },
|
||||||
|
{ defaultToken: "string" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
token: "string.start",
|
||||||
|
regex: "'",
|
||||||
|
stateName: "singleQuoteString",
|
||||||
|
next: [
|
||||||
|
{ token: "string", regex: /\\\s*$/, next: "singleQuoteString" },
|
||||||
|
{ token: "constant.language.escape", regex: escapeRe },
|
||||||
|
{ token: "string.end", regex: "'|$", next: "start" },
|
||||||
|
{ defaultToken: "string" },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
token: "constant.numeric",
|
||||||
|
regex: /\d+\.?\d*[eE]?[\+\-]?\d*/,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
token: "variable",
|
||||||
|
regex: /\$[_A-Za-z][_0-9A-Za-z]*/,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
this.normalizeRules()
|
||||||
|
}
|
||||||
|
|
||||||
|
oop.inherits(GQLQueryTextHighlightRules, TextHighlightRules)
|
||||||
|
|
||||||
|
exports.GQLQueryTextHighlightRules = GQLQueryTextHighlightRules
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// Language Mode Definition
|
||||||
|
ace.define(
|
||||||
|
"ace/mode/gql-query",
|
||||||
|
["require", "exports", "ace/mode/text", "ace/mode/gql-query-highlight"],
|
||||||
|
(aceRequire, exports) => {
|
||||||
|
const oop = aceRequire("ace/lib/oop")
|
||||||
|
const TextMode = aceRequire("ace/mode/text").Mode
|
||||||
|
const GQLQueryTextHighlightRules = aceRequire("ace/mode/gql-query-highlight")
|
||||||
|
.GQLQueryTextHighlightRules
|
||||||
|
const FoldMode = aceRequire("ace/mode/folding/cstyle").FoldMode
|
||||||
|
|
||||||
|
const Mode = function () {
|
||||||
|
this.HighlightRules = GQLQueryTextHighlightRules
|
||||||
|
this.foldingRules = new FoldMode()
|
||||||
|
}
|
||||||
|
|
||||||
|
oop.inherits(Mode, TextMode)
|
||||||
|
|
||||||
|
exports.Mode = Mode
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user