How can I change text colour in sublime text? - text

I want to color source code as i want. For example, if I have this code
<script>
var x = 1;
function yu() {
document.getElementById("e").innerHTML = "O";
}
</script>
I want to color the whole function ( lines 3,4,5), or color just a line or a word. I don't want sublime text to color every word depending of the type of the words.
If it cant be done in sublime text, do you know other source code editros or IDE which an do that?

You can write a custom syntax which meets your criteria and set it as the default syntax for any filetypes you wish to affect.
Here is a basic syntax example which affects files with the extension .MyExtension
Save the following code #:
Packages/MySyntaxFiles/MyCustomSyntax.sublime-syntax
%YAML 1.2
---
name: MyCustomSyntax
file_extensions: [ MyExtension ]
scope: source.MyScopeName
contexts:
main:
- match: '\bfunction.*?{'
push: function
- match: '\b<.+?>\b'
scope: entity.name.tag
function:
- meta_scope: entity.name.function
- include: braces
braces:
- match: '{'
push: braces
- match: '}'
pop: true
Note: This example meets the minimal requirements of the code sample you provided. You will likely need to elaborate upon this template in order for it to fully meet your actual usage requirements.
Resources:
SublimeText > Official Documentation > Syntax
SublimeText > Unofficial Documentation > Syntax Definitions
ScopeHunter
Use this to find scopes that you might want to replicate
PackageResourceViewer
Use this to open existing sublime-syntax files for reference
Color Schemes
Browse the included tmTheme files to view commonly used scopes.
For maximum customization: create your own tmTheme file to match your syntax & scopes.
RegExr
You will need a basic knowledge of RegEx in order to define scope matches.
RegExr has an excellent Reference section which covers most of the basics, along with a testing area so you can try things out as you learn.

Related

How to embed inline C++ syntax in another language definition?

I have a custom Perl syntax file for Sublime Text 3 and I would like to add an highlighting support for embedded C++ code.
The inline C++ code always starts with __CPP__ and ends with __END__ among the perl code.
I'm using the 'embed/match/escape' approach but the syntax highliting of the C++ code won't change (getting the scope with ctrl-alt-shift-p it still appears as source.perl).
Here the rule I'm using in my syntax file:
contexts:
main:
- include: inline-cpp
...lot of rules...
inline-cpp:
- match: '__CPP__'
embed: scope:source.c++
embed_scope: source.c++.embedded
escape: '__XXX__'
Please could someone help me figure out what I'm doing wrong?
Solved putting the include in prototype section instead of main :
contexts:
main:
...
prototype:
- include: inline-cpp
inline-cpp:
- match: '__CPP__'
embed: scope:source.c++
embed_scope: source.c++.embedded
escape: '__XXX__'

Sublime Text 3 custom syntax for cottle: hard to start

I'm trying to make a "very simple" syntax highlight for "cottle" (which is a script language used in a text-to-speech app dedicated to Elite:Dangerous).
All i want (at least at the beginning) is to have three different colours: Comments, "non-strings", and strings.
I started trying with the ST3 wiki, youtube tutorials, questions here.... but i can't sort out how to do it, 'cause the way the language work.
I'll try to show you an example
{ everything_between_a_pair_of_brackets_is_code }
everything outside all pairs of bracket is a string {_ and this is a comment. It begins with "_" and ends at the closing bracket }
{ This_is_code("but this is a string")
This_is_still_code("this is also a string {but_this_is_code(\"and a string\")} and this the end of the string")
}
My problem is how to define this kind of "nidification" in my cottle.sublime-syntax file. I managed to get the comment, but only the first one.
- EDIT -
This is a real script:
{event.item}
{if event.repairedfully:
fully repaired
|else:
partially repaired
{Occasionally(2,
cat(
OneOf("to ", "at "),
Humanise(event.health * 100),
" percent functionality"
)
)}
}
{Occasionally(2,
cat(OneOf(", ", "and is"), " ready for re-activation")
)}.
The output of this script could be "Engine module fully repaired." or "Engine module partially repaired, and is ready for re-activation."
Please note the last dot of the phrase, which in the code is after the last bracket.
This is another sample, with strings passed to functions inside other strings:
{OneOf("{ShipName()} has", "")}
{OneOf("left supercruise", "{OneOf(\"entered\", \"returned to\", \"dropped to\")} normal space")}
My question is:
how sublime-syntax files handle this kind of nidification?
Looking at the overview of the templating language over at https://cottle.readthedocs.io/en/stable/page/01-overview.html, it seems to be an easy syntax for which to write a .sublime-syntax for, but given the utter lack of resources for knowing how syntax files works in ST, I can understand it can be sometimes difficult to start or even understand.
So, I took the liberty of creating a starter syntax definition (the result of an hour & a half of boredom on a Saturday evening), which you can take and work upon. Note that I have not used the language and as such made it by just reading the docs and looking over code snippets.
You can find a gist for it here (https://gist.github.com/Ultra-Instinct-05/96fa99e1aaeb32b12d1e62109d61fcc2)
Here is a screenshot showing it in the color scheme I use (which follows the official scope naming guidelines).
It still lacks support for user defined functions (as I came to know from the docs) (and probably a few other things), but maybe that's something you can add to it !
Note that to use it, save the file as Cottle.sublime-syntax in your User package. Right now files having a .cottle extension are highlighted (because I don't know how you create a cottle file).
The syntax definition doesn't use any new feature added in ST4, so it should work the same in both ST3 & ST4.

Android Studio(flutter project) - how find all cyrillic's string resource(for localization)?

I am working with an existing project. In this project all string resources are "scattered" in the code, example:
child: Text('ั‚ะตัั‚'),
now I need to add localization for these strings - then find them. these lines are symbols in Cyrillic.
the first option I try is to find these characters using regular expressions. i used the following options:
[\p{IsCyrillic}]
\p{IsCyrillic}+
but this does not find all the words in Cyrillic. Perhaps you can suggest other options (another regular expression)? I would love to automate this - but I don't know how yet.

How to correctly embed another language into my current language defintion?

I have Smalltalk sublime-syntax file (YAML) for Sublime Text 3 and I would like to add an highlighting support for embedded C code.
The inline C (which always starts with ^%\{ and ends with %\}$) code among the smalltalk code.
A simple example (not much C but wanted a simple case):
sigABRT
"return the signal number for SIGABRT - 0 if not supported by OS
(the numeric value is not the same across unix-systems)"
%{ /* NOCONTEXT */
#ifdef SIGABRT
RETURN ( __mkSmallInteger(SIGABRT) );
#else
RETURN ( __mkSmallInteger(0) );
#endif
%}
!
There is new feature embed in Sublime text (with even an example).
I tried to do something like this:
- match: '^%\{'
embed: scope:source.c
embed_scope: meta.environment.embedded.c.smalltalk source.c.embedded
escape: '%\}$'
However, I was unable to correctly incorporate it into my current highlighting file.
Does anyone know how to correctly embed one language to another?
This question is a little sticky because you've provided a sample syntax definition and a sample of some Smalltalk source code, but the code provided is not highlighted by the provided syntax because it's not structured properly.
For our purposes here, lets assume that the Smalltalk sample you provided is the following one. This may or may not be valid (it's been a long time since I've worked with Smalltalk) but it highlights with your syntax, so lets call that good enough for testing purposes.
Object subclass: Test [
sigABRT
"return the signal number for SIGABRT - 0 if not supported by OS
(the numeric value is not the same across unix-systems)"
%{ /* NOCONTEXT */
#ifdef SIGABRT
RETURN ( __mkSmallInteger(SIGABRT) );
#else
RETURN ( __mkSmallInteger(0) );
#endif
%}
!
].
The syntax match that you provided above is the correct one to use, so I'm guessing that your problem is in where you placed it in the syntax.
So lets presume that there is more than one place where we might want to match one of these C blocks in the syntax definition; in that case we may want to create a new context in the syntax that contains the match so that we can include it where it's needed:
c-block:
- match: '%\{'
embed: scope:source.c
embed_scope: meta.environment.embedded.c.smalltalk source.c.embedded
escape: '%\}$'
This is the same excerpt as you provided above, but placed into a context. So lets say that the first place where a block such as this can appear is in the body of a block. You have a block-body context in your syntax, so we stick an include onto the end of it to include this new context:
block-body:
- include: pragma
- include: selector
- include: literal
- include: block
- include: comment
- include: c-block
However, this does not have the desired outcome; the highlight is not correct:
Clearly the highlighting is going wrong starting at least at the C comment start, possibly earlier. If you use Tools > Developer > Show Scope Name while the cursor is on the comment, you can see that the scope assigned is source.smalltalk entity.name.function, which means that the syntax is treating the C comment start as a method name.
It also looks like the %{ construct is not properly highlighted, and a check shows that the scope of the % character is source.smalltalk keyword.other.
So in reality the problem currently is that with the above definitions in place, instead of seeing %{ as starting a C block, it's being seen as a keyword, and if it's a keyword then the rules for matching a C block are not triggering at all.
If you look at your syntax, the main context looks like this:
main:
- match: '([a-zA-Z][a-zA-Z0-9]*)\s*(subclass:)\s*([a-zA-Z][a-zA-Z0-9]*)\s*\['
captures:
1: entity.other.inherited-class
2: keyword.other
3: entity.name.type
push:
- match: '\]'
pop: true
- include: pragma
- match: '(([a-zA-Z][a-zA-Z0-9]*:)|[+\-\/\\*~<>=#%|&?!.,:;^]+)\s*([a-zA-Z][a-zA-Z0-9]*)'
captures:
1: entity.name.function
3: variable.other
- match: "([a-zA-Z][a-zA-Z0-9]*)"
scope: entity.name.function
- include: block
- include: comment
- include: block-body
These rules say that when we see a line that starts with something like BaseClass subclass: SubClass [, we are entering into an anonymous context (via the push) to handle the contents of the class body (or block or whatever).
The anonymous context contains the rule to pop out when it sees the closing ] character, two different matches to find a function name, then an include on the contexts for block, comment and block-body respectively.
When you include, Sublime takes all of the match rules from that context and inserts a copy of them at the point where you do the insert, as if you had just manually entered them there.
In addition, when there is more than one rule in a context that might potentially match, the first match rule in the context is the one that is applied (i.e. it "wins" the tie).
The scope keyword.other is applied in the rules from the pragma context as well as the selector context, and the selector context can match single % character as a keyword.
Thus the problem here is that since the include c-block appears after selector in the include list for the block-body context, the selector context is finding and matching the % character before the rule for the C block can find it.
The solution then would be to shift the location of the include c-block to be prior to that item to make sure that it matches first:
block-body:
- include: c-block
- include: pragma
- include: selector
- include: literal
- include: block
- include: comment
With that in place, the block highlights more like we would expect:
I was able to work around a similar issue with Regex lookahead and lookbehinds
- match: ' (?=\{")'
embed: scope:source.json
escape: '(?<=\})$'

Can JAPE match paragraph Annotation in LHS?

I'm working on a math word problem solver, and would like to pass whole problems to my GATE Embedded application using JAPE. I'm using GATE IDE to display the output, as well as run the pipeline of GATE components. Each problem will be in its own paragraph, and each document will have several problems on it.
Is there a way to match any paragraph using the JAPE left-hand side regex?
I see three options here (there may be more elegant solutions):
1) Use simple rule like:
Phase: find
Input: Token
Options: control = once
Rule:OneToken
(
{Token}
)
In RHS you could get a text and use standard Java approach for getting paragraphs from plain text.
2) Use LHS (if you really want only LHS)
Rule: NewLine
(
({SpaceToken.string=="\n"}) |
({SpaceToken.string=="\r"}) |
({SpaceToken.string=="\n"}{SpaceToken.string=="\r"}) |
({SpaceToken.string=="\r"}{SpaceToken.string=="\n"})
):left
Build annotation NewLine, then write a Jape rule similar to 1) but with NewLine instead of Token. Take all NewLines from outputAS and build your Paragraph annotations.
3) Sometimes there may be right paragraphs in Original markups. In this case you could use Annotation Set Transfer PR and get them in Default Annotations Set.
why not just use RegEx Sentence splitter PR to use Split as the Input in your jape rules?

Resources