-
-
Notifications
You must be signed in to change notification settings - Fork 603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use custom mode #126
Comments
Hi, and what's your implementation? I can show what works for me. I don't remeber it exactly, but I think I had to install also npm install -D brace react-ace And this is my component with Ace editor:
|
I could use the default modes from brace indeed. What I wonder is how to use my own mode? I wrote a custom mode by myself and want to include that, but was not able to do that with react-ace so far. |
@sichvoge Did you make any progress on this at all? |
Not really. In the end I developed my own react component using Ace and not brace. |
Fair enough - perhaps you could share it when you're happy with it? Thanks @sichvoge |
Sure, I will. |
@sichvoge , can you please share your component? |
I am trying to do a similar thing. Importing the mode in the front end doesn't seem to work for me. So say my custom mode is called 'eikelang' My React code looks like this import React from 'react';
import AceEditor from 'react-ace';
import 'brace/theme/github';
export default ({value}) => {
<AceEditor
mode="eikelang"
theme="github"
width="auto"
height="200px"
value={value}
/>
}; I have a js file called 'mode-eikelang.js' served from my server's root directory, which is just a rip off of the mysql brace mode file with some names changed: ace.define('ace/mode/doc_comment_highlight_rules',
['require', 'exports', 'module', 'ace/lib/oop', 'ace/mode/text_highlight_rules'],
function(acequire, exports, module) {
'use strict';
let oop = acequire('../lib/oop');
let TextHighlightRules = acequire('./text_highlight_rules').TextHighlightRules;
var DocCommentHighlightRules = function() {
this.$rules = {
'start': [{
token: 'comment.doc.tag',
regex: '@[\\w\\d_]+' // TODO: fix email addresses
},
DocCommentHighlightRules.getTagRule(),
{
defaultToken: 'comment.doc',
caseInsensitive: true
}]
};
};
oop.inherits(DocCommentHighlightRules, TextHighlightRules);
DocCommentHighlightRules.getTagRule = function(start) {
return {
token: 'comment.doc.tag.storage.type',
regex: '\\b(?:TODO|FIXME|XXX|HACK)\\b'
};
};
DocCommentHighlightRules.getStartRule = function(start) {
return {
token: 'comment.doc', // doc comment
regex: '\\/\\*(?=\\*)',
next: start
};
};
DocCommentHighlightRules.getEndRule = function(start) {
return {
token: 'comment.doc', // closing comment
regex: '\\*\\/',
next: start
};
};
exports.DocCommentHighlightRules = DocCommentHighlightRules;
});
ace.define('ace/mode/eikelang_highlight_rules', ['require', 'exports', 'module', 'ace/lib/oop', 'ace/lib/lang', 'ace/mode/doc_comment_highlight_rules', 'ace/mode/text_highlight_rules'], function(acequire, exports, module) {
let oop = acequire('../lib/oop');
let lang = acequire('../lib/lang');
let DocCommentHighlightRules = acequire('./doc_comment_highlight_rules').DocCommentHighlightRules;
let TextHighlightRules = acequire('./text_highlight_rules').TextHighlightRules;
let EikelangHighlightRules = function() {
let builtins = 'drop|hash|keep|linear_transformation|map|rename|string_manipulation|string_submatcher';
let keywordMapper = this.createKeywordMapper({
'support.function': builtins
}, 'identifier', true);
function string(rule) {
let start = rule.start;
let escapeSeq = rule.escape;
return {
token: 'string.start',
regex: start,
next: [
{token: 'constant.language.escape', regex: escapeSeq},
{token: 'string.end', next: 'start', regex: start},
{defaultToken: 'string'}
]
};
}
this.$rules = {
'start': [{
token: 'comment', regex: '(?:-- |#).*$'
},
string({start: '"', escape: /\\[0'"bnrtZ\\%_]?/}),
string({start: '\'', escape: /\\[0'"bnrtZ\\%_]?/}),
DocCommentHighlightRules.getStartRule('doc-start'),
{
token: 'comment', // multi line comment
regex: /\/\*/,
next: 'comment'
}, {
token: 'constant.numeric', // hex
regex: /0[xX][0-9a-fA-F]+|[xX]'[0-9a-fA-F]+'|0[bB][01]+|[bB]'[01]+'/
}, {
token: 'constant.numeric', // float
regex: '[+-]?\\d+(?:(?:\\.\\d*)?(?:[eE][+-]?\\d+)?)?\\b'
}, {
token: keywordMapper,
regex: '[a-zA-Z_$][a-zA-Z0-9_$]*\\b'
}, {
token: 'constant.class',
regex: '@@?[a-zA-Z_$][a-zA-Z0-9_$]*\\b'
}, {
token: 'constant.buildin',
regex: '`[^`]*`'
}, {
token: 'keyword.operator',
regex: '\\+|\\-|\\/|\\/\\/|%|<@>|@>|<@|&|\\^|~|<|>|<=|=>|==|!=|<>|='
}, {
token: 'paren.lparen',
regex: '[\\(]'
}, {
token: 'paren.rparen',
regex: '[\\)]'
}, {
token: 'text',
regex: '\\s+'
}],
'comment': [
{token: 'comment', regex: '\\*\\/', next: 'start'},
{defaultToken: 'comment'}
]
};
this.embedRules(DocCommentHighlightRules, 'doc-', [DocCommentHighlightRules.getEndRule('start')]);
this.normalizeRules();
};
oop.inherits(EikelangHighlightRules, TextHighlightRules);
exports.EikelangHighlightRules = EikelangHighlightRules;
});
ace.define('ace/mode/eikelang', ['require', 'exports', 'module', 'ace/lib/oop', 'ace/mode/text', 'ace/mode/eikelang_highlight_rules'],
function(acequire, exports, module) {
let oop = acequire('../lib/oop');
let TextMode = acequire('../mode/text').Mode;
let EikelangHighlightRules = acequire('./eikelang_highlight_rules').EikelangHighlightRules;
let Mode = function() {
this.HighlightRules = EikelangHighlightRules;
this.$behaviour = this.$defaultBehaviour;
};
oop.inherits(Mode, TextMode);
(function() {
this.lineCommentStart = ['--', '#']; // todo space
this.blockComment = {start: '/*', end: '*/'};
this.$id = 'ace/mode/eikelang';
}).call(Mode.prototype);
exports.Mode = Mode;
}); |
Hi @pollen8, Meaning that you have to make sure your mode is already in "Ace" cache using So I:
My custom mode is:
And my react-ace code looks like:
The only downside it see is depending on |
@AlonBe @pollen8 Tried following the directions that @AlonBe listed with the custom mode provided by @pollen8 and I cannot unfortunately get the syntax highlighter to register correctly... :( Any help would be appreciated. Where my editor is being rendered:
|
@solemnify, Did you try to do If you want to use syntax highlighter you need to override Take a look at one of the current modes such as sql_mode |
@AlonBe |
The following code worked for me: App.js
CustomSqlMode.js
The previous code highlights the comments and strings only. I'm not sure if what I did was the good practice or not, but at least it works. |
@newint33h thanks!It works for me. |
@newint33h I tried to use this structure but it complains that TypeError: Cannot read property 'editor' of undefined. |
@shuotongli I had the same problem and it looks like that issue is because React's |
why the mode='text', what does text mean? |
@TaurusWood, as the first and BTW, if I'm not getting wrong, mode='text' is the basic mode of Ace that all the others are overriding |
Can anyone explain why is't it working anymore, all of my codes are interpreted as a text instead of java file after setting a custom mode? Thanks
|
Is there any update regarding this issue? Looks like it @newint33h example doesn't work anymore... |
@danielpaz6 RSOCMode.js import 'brace/mode/text';
export class CustomHighlightRules extends window.ace.acequire("ace/mode/text_highlight_rules").TextHighlightRules {
constructor() {
super();
this.$rules = {
"start" : [
{
token : "comment",
regex : /^~.*$/
},
{
token : "variable",
regex : /:.*$/
},
{
token : "keyword",
regex : /(?:set|add|show|ifg)\b/,
caseInsensitive: true
},
{
token : "constant.numeric",
regex : /[0-9]+\b/,
}
]
};
}
}
export default class RSOCMode extends window.ace.acequire('ace/mode/text').Mode {
constructor() {
super();
this.HighlightRules = CustomHighlightRules;
}
} MyEditor.jsx const aceEditorRef = useRef(null);
useEffect(() => {
const rsocMode = new RSOCMode();
if(aceEditorRef.current != null){
console.log(aceEditorRef);
aceEditorRef.current.editor.session.setMode(rsocMode);
}
}, [aceEditorRef.current])
return (
<AceEditor
ref={aceEditorRef}
mode="text"
theme="monokai"
editorProps={{ $blockScrolling: true }}
showPrintMargin={false}
fontSize={26}
value={codeValue}
onChange={(val) => {}}
style={{
width: '100%',
borderRadius: '8px',
}}
/>
); I hope this helps :) |
I'm sorry this sounds basic, but despite adding the dependency, I still don't know why window does not have ace property yet whenever I try to extend
|
Hi,
My question might be a bit stupid (apologies for that), but I can't figure out how to use a custom mode / syntax highlighter / auto-completion. Can anyone help me? I am very new to brace, ace, and react but I saw some materials on how to create and set your custom mode using Ace. What I couldn't figure out is how to do the same with react-ace.
Christian
The text was updated successfully, but these errors were encountered: