PersistentRegexHighlight -- a more detailed example please - sublimetext2

Using the existing documentation as a guide, would it be possible for someone to please give me a more detailed example of implementing the PersistentRegexHighlight plugin with a specific syntax (e.g., LaTeX, or a different syntax, whatever is easier). Perhaps we could do something simple, like underline and change the color of [Ff]oo.
I'm confused regarding how to determine what [Ff]oo is. In my regular theme file, <string>Keyword</string> affects the color for most of the LaTeX codes. In the LaTeX.tmLanugage file, there are seven keys mentioned in the beginning -- e.g., keyword.control.preamble.latex. My best guess at this point is that [Ff]oo could be a pre-defined language file key such as keyword.control.preamble.latex.
I am assuming that this regex code goes inside PersistentRegexHighlight.sublime-settings, or, perhaps the user syntax specific file (e.g., LaTeX.sublime-settings).
{
"regex": [{
"pattern": "[Ff]oo",
"color_scope": "color.scope.name",
"ignore_case": true
}, {
"pattern": "constant.language",
"color_scope": "constant.language"
}]
"regex": [{
"pattern": "Bar",
"color": "00FF00",
"ignore_case": false
}]
}
I am assuming that this code goes into the user designated theme file. I do not understand where we are getting regex.highlight.one, because it is not referenced within PersistentRegexHighlight.sublime-settings.
<dict>
<key>name</key>
<string>Regex Highlight</string>
<key>scope</key>
<string>regex.highlight.one</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#75715E</string>
</dict>
</dict>

The necessary disclaimer. I'm the author/maintainer of the PersitsentRegexHighlight plugin. Now that that's done here is a better example.
I thought I had included an example with "underline" but I guess I forgot. For underlining, you would add an "underline" key in the regex pattern. If you are using ST2, you cannot specify an underline style. As for color, you can specify one of two ways. The first is specifying a hex with the key color. This will create a new color scheme file, so this may not be desirable. The second method is to specify a scope. If you look at your tmTheme file, you will see a list of entries. One of the keys is scope, which is used as the color_scope name. The following exaample will highlight the regular expression [Ff]oo the same color as a string and underline it.
{
"regex": [{
"pattern": "[Ff]oo",
"color_scope": "string",
"underline": true
}]
}
These settings are placed either in PersistentRegexHighlight.sublime-settings or can be created to be project specific.
I would recommend taking a look at a tmTheme file, to gather a better understanding of what it is doing. Of course, feel free to ask more questions.

Related

Aliasing address of json schema in pointers to avoid duplication

Is there a way to somehow alias / rename an external schema address to avoid duplicating it in pointers throught a schema?
e.g. consider something along the following lines:
blah://some.url/my-common-schema-version-12.json
{
"definitions":[
"schema1":{},
"schema2":{},
]
}
derived-schema.json
{
"definitions":[
"thing1":{
"$ref":"blah://some.url/my-common-schema-version-12.json#/definitions/schema1"
},
"thing2":{
"$ref":"blah://some.url/my-common-schema-version-12.json#/definitions/schema2"
},
]
}
Ideally it would be good to avoid repeating the 'blah://some.url/my-common-schema-version-12.json' bit in derived-schema.json and have a single place where it can be changed.
Having a search around I can't see anything obvious but perhaps there is a way to do it?
Yes. You define $id at the root of your schema. References are relative to the current "Base URI", which is that defined in the closest $id value.
Relative references must first be "resolved", which means determining the full address against the initial Base URI (and not finding and fetching the resource, as many believe).
This is very similar to how the anchor tag works in HTML. The value of a href attribute of the anchor tag is resolved against the Base URI. So if you write HTML, you're probably already familiar with how this works.

VScode - replace captured group with the values

i have a bunch of strings in code such as:
<td style="background-color:#fdfdff"> </td>
and
<td> </td>
in one file.
The goal is to replace from first example with 0, while from second example with - (dash)
I'm using VScode regex, but I can't find the way to replace captured groups with specific values, as $1, $2 groups refer to original string groups.
This one is just example, how I'm trying to achieve this, but VScode don't ignore grouped regex.
An alternative process is to use a snippet which can do conditional replacements. With this snippet:
"replaceTDs": {
"prefix": "tdr", // whatever prefix you want
"body": [
"${TM_SELECTED_TEXT/(?<=\">)( )|( )/${1:+0}${2:+-}/g}",
]
}
The conditional replacements can be quite simple since you first find and select only the two alternative texts you are interested in. So
find: <td\s*(style="[^"]*"\s*)> </td>|<td> </td> old version
This simpler find will probably work for you:
<td\s*(style="[^"]*")?\s*> </td>
Don't replace, rather Control+Shift+L : selects all your two alternatives. Esc to focus on editor from the find widget.
Then apply your snippet, in this case type tdr+Tab
and all the changes are made. You just have to make the snippet one time and then do a single find.
This technique scales a little better than running as many find/replaces as you have replacements to do. Even with more conditional replacements it would probably be a simple change to the one snippet to add more replacements.
Also you can simplify this even more if you use a keybinding to trigger your snippet (you don't have to change focus from the find widget - or create the separate snippet). So with no snippet, but this keybinding:
{
"key": "alt+w",
"command": "editor.action.insertSnippet",
"args": {
"snippet": "${TM_SELECTED_TEXT/(?<=\">)( )|( )/${1:+0}${2:+-}/g}"
},
"when": "editorHasSelection"
}
now the same demo:
You can use
Search for (?<=<td\s+style="[^"]*">) (?=</td>) and replace with 0, and
Search for <td> </td> and replace with <td>-</td>, no need for a regex here.
Note that capturing groups are meant to keep captured substrings.
The first pattern matches
(?<=<td\s+style="[^"]*">) - a place in string that is immediately preceded with <td, one or more whitespaces, style=", any zero or more chars other than " and then a >
- a literal string
(?=</td>) - immediately to the right, there must be </td>.

How to force prettier html formatting to format tags in one line?

I use prettier in my VSC, so how to force prettier HTML formatting to format tags in one line, not multiple lines?
I want to format something like this all in one line
<v-navigation-drawer :clipped="$vuetify.breakpoint.lgAndUp" v-model="drawer" fixed app>
Does exist any config for prettier HTML formatter?
Prettier has the option printWidth. If you set that option to a high number, it will break fewer lines.
In your .prettierrc.json file, you can override this option for your HTML files:
{
// Other options...
"overrides": [
{
// change .html with .vue if you are using Vue files instead of HTML
"files": "src/**/*.html",
"options": {
"printWidth": 140
}
}
]
}
Is not recommendable to use a line-lenght higher than 140. Prettier is opitionated, so it should be used without too many customizations.
First check if you have a filename .prettierrc in the root. If no, create it and then put these values in it.
{
"trailingComma": "es5",
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"printWidth": 3000,
"bracketSpacing": true
}
Set printWidth to something large value and toggle wrapping text with your editor whenever you want; for example, in VS Code: Alt+Z.
I know this topic is old but maybe others still have this issue. In the Prettier setting you can adjust the width of the Print Width. 120 width does the job for me.
It depends on your IDE settings but as documentation states, you should have a config file where you should look for the html.format.wrapLineLength property and apply the line length value which satisfy you. This will prevent Prettier to break your lines until assigned line length to the property is reached. This will only apply for HTML.
For overall purposes you should use prettier.printWidth property which
Fit code within this line limit
Mind that tabulation / indention spaces also count in the line length.
Unfortunately when code line exceed the above numbers it will be spited to multi-lines, each property per line. So far I did not find solution to just wrap to extra line, without spiting to multi.
UPDATE
Mentioned and desired by me behavior it is not affordable with Prettier, but the line length trick still can do the job ... partially.

Extract and Create Property in JSON file with RegEx

I have the following JSON file. Dotted across the file is the following:
"properties": {
"Name": "Darlington",
"Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}
Using RegEx, I would like to extract the CCG Code property and add it back in so that the above becomes:
"properties": {
"Name": "Darlington",
"CCGcode": "00C",
"Description": "<br><br><br> <table border=\"1\" padding=\"0\"> <tr><td>CCGcode</td><td>00C</td></tr> <tr><td>CCGname_short</td><td>Darlington</td></tr>"
}
I've tried all sorts and I just can't get it to work. I am using Sublime Text.
^("Description":").*?<td>CCGcode<\/td><td>([^<>\n]*).*$
The above selects the code, but not sure how I can get it to create the property.
Try this
( *)"Description".*?CCGcode.*?<td>([^<]+)
Regex demo
This one for sublimetext3
Find what:
( *)("Description".*?CCGcode.*?<td>)([^<]+)
Replace with:
\1"CCGcode": "\3",\n\1\2
Demo
There's a very simpel, but not so elegant, solution. Replace
"Description":.*?<td>CCGcode<\/td><td>([A-Z\d]*)<\/td>
with
"CCGCode":"\1",\n \0
Don't know how Sublime handles replacements, but you may have to change the replacing \0 and \1 to something else - e.g. $0 and $1.
What it does is to find the Description entry and the following CCGCode entry, capturing the code into capture group 1.
Then replace capture group 0 - the whole matched text, with the new CCGCode JSON tag plus the original text.
It's a pretty fragile solution, but it works for your sample case.
Check out example at regex101.
Regards

Structured text in JSON

I have been looking for a way to capture structured text (sections, paragraphs, emphasis, lists, etc.) in JSON, but I haven't found anything yet. Any suggestions? (Markdown crossed my mind, but there might be something better out there.)
How about something like this:
[ { "heading": "Foobar Example" },
{ "paragraph":
[
"This is normal text, followed by... ",
{ "bold": "some bold text" },
"etc."
]
}
]
That is:
use a string for plain text without formatting or other mark-up;
use an array whenever you want to indicate an ordered sequence of certain text elements;
use an object where the key indicates the mark-up and the value the text element to which the formatting is applied.
HTML is a well-established way to describe structured text, in a plain-text format(!). Markdown (as you mentioned) would work as well.
My view is that your best bet is probably going to be using some sort of plain-text markup such as those choices, and place your text in a single JSON string variable. Depending on your application, it may make sense to have an array of sections, containing an array of paragraphs, containing an array of normal/bold/list sections etc. However, in the general case I think good old-fashioned blocks are markup will ironically be cleaner and more scalable, due to the ease of passing them around, and the well-developed libraries for full-blown parsing if/when required.
There also seems to be a specification that might accomplish this Markdown Syntax for Object Notation (MSON)
Not sure if for you it's worth the trouble of implementing the spec, but it seems to be an option.