Codemirror diff - diff

I want to implement diff mode using codemirror (not the merge tool). something like: git diff. I used Codemirror official diff mode, however, not able to get the tokenizer (+, -) when the user has added or changed something.

The CodeMirror Diff Mode just highlights a row based on its format (e.g. it highlights a row with a green colour if it starts with "+" or with red colour if it starts with "-"). You need to generate the diff text by yourself. There are several libraries which you can use for it, for example jsdiff
You can get the diff between 2 texts like this:
import * as Diff from 'diff';
var text1=`
line1
line2
line3
`;
var text2=`
line0
line3
line4
line5
`;
var diff = Diff.createTwoFilesPatch('old', 'new', text1, text2, { context: 1 });
console.log('diff', diff);
It will produce the following diff:
diff ===================================================================
--- old [object Object]
+++ new
## -1,4 +1,7 ##
-line1
-line2
+line0
+
line3
+
+line4
+line5
And after is you can insert this text into CodeMirror.

Related

Is it possible to have a snippet that considers the length of my input?

I would like to define a snippet for comments like
//************
//* foo A1 *
//************
where I enter foo A1 and it would create a line with (6+ len(${1}) asterisks etc. - is that doable and if so, how?
While I am a big proponent of HyperSnips (see
[VSCODE]: Is there a way to insert N times the same characters,
VS Code: how to make a python snippet that after string or expression hitting tab will transform it and
VSCode Advanced Custom Snippets for how to use it),
it is instructive to see how to do this with just the built-in snippet functionality in vscode. Here is a snippet that does what you want:
"Custom Comment": {
"prefix": ["cc2"], // whatever trigger you want, then tab, write your info and tab again
"body": [
"//***${1/./*/g}***",
"//* $1 *",
"//***${1/./*/g}***"
]
},
That just adds 3 asterisks to the beginning and 3 to the end of your added comment, each character of which is replaced by an asterisk as well.
You can use the extension HyperSnips
snippet comment "Comment" A
``rv = '//' + '*'.repeat(t[0].length + 6)``
//* $1 *
``rv = '//' + '*'.repeat(t[0].length + 6)``
endsnippet

VSCode: Extension: folding section based on first blank line found or to the start of the next similar section

How can I make a VSCode extension folding strategy based on the first blank line following a starting folding marker?
## Some section --|
Any text... | (this should fold)
...more text. --|
(blank line)
## Another section (next fold...)
I've tried lots of regex in the language-configuration.json.
"folding": {
"markers": {
"start": "^##",
"end": "^\\s*$"
} },
If I change things to test with something other than a blank (or whitespace) line as the end delimiter it works. Can't use the next start marker to mark the end of the last or it includes it in the fold (I tried look ahead regex, but I think the regex are applied line by line and the matches can't span lines?)
It's similar to the folding needed for Markdown which VSCode handles well (don't know if that's using a more complex method like https://code.visualstudio.com/api/references/vscode-api#FoldingRangeProvider).
Maybe something in the fixes for [folding] should not fold white space after function has something to do with it.
What I learned: 1. the begin and end regex are applied line by line. 2. tmLanguage start/end regex will work on blank lines, but currently language-configuration folding doesn't seem to work on blank lines.
And since blank lines are in this case a hack for ending at the next begin section:
To solve the problem of folding a section to the next similar section I used the FoldingRangeProvider.
disposable = vscode.languages.registerFoldingRangeProvider('myExt', {
provideFoldingRanges(document, context, token) {
//console.log('folding range invoked'); // comes here on every character edit
let sectionStart = 0, FR = [], re = /^## /; // regex to detect start of region
for (let i = 0; i < document.lineCount; i++) {
if (re.test(document.lineAt(i).text)) {
if (sectionStart > 0) {
FR.push(new vscode.FoldingRange(sectionStart, i - 1, vscode.FoldingRangeKind.Region));
}
sectionStart = i;
}
}
if (sectionStart > 0) { FR.push(new vscode.FoldingRange(sectionStart, document.lineCount - 1, vscode.FoldingRangeKind.Region)); }
return FR;
}
});
Set "editor.foldingStrategy": "auto". You can make it more sophisticated to preserve white space between sections.

Multiline column copy paste in VS Code

Is it possible to do pasting in multiline editing (cursor |):
text1 = [|]
text2 = [|]
text3 = [|]
text4 = [|]
Assuming I have pasted the following lines:
val1
val2
val3
val4
I would like to have this result:
text1 = [val1]
text2 = [val2]
text3 = [val3]
text4 = [val4]
What actually happens is that the clipboard content is pasted four times, once for each cursor.
Something like mentioned in this answer, but instead of typing simply pasting: https://stackoverflow.com/a/30039968/1374488
Use column-edit instead of the multi-line edit mode:
Click the end of the source text.
Shift Alt, click the beginning.
Copy.
Click the end of the destination text.
Shift Alt, click the beginning.
Paste.
I had some trouble with this until I figured it out. The second selection ( where you want to paste ), must be the same length as the first selection, otherwise it pastes all items at each location ( instead of one item per row ).
1-select column of data you want to copy by holding alt+shift+mouse selection box and copy it with ctrl+c
2- select the places you want to paste into with alt+mouse click(note: this helps if the lines to be pasted into are in different places)
3-paste into the selected locations with ctrl+v
I had to do this for hundreds of lines, mapping db columns.
What I ended up doing to speed this is was creating an excel sheet with 3 columns:
COL1 COL2 COL3
text1 = [ val1 ]
text2 = [ val2 ]
text3 = [ val3 ]
text4 = [ val4 ]
And then searching and replacing tabs.
Worked for me https://github.com/john-guo/columnpaste . Adds Column paste command.

How do I get word-based movements in emacs go-mode to not skip over backquoted strings?

If I have code like so:
func main() {
a := `line 1
line 2
line 3
line 4`
fmt.Println(a)
}
doing forward-word or backword-word when the cursor is
in the multi-line string moves to the end or the start
of the string respectively. I'm using go-mode.el v1.3.1
from https://github.com/dominikh/go-mode.el
Similar if the cursor is inside the string and you do
I tried doing
(modify-syntax-entry ?` ".")
in the go-mode-hook but that didn't change the behavior.

MSWord macro: search and highlight formatted text patterns

I'm trying to write a MSWord macro that will find, and then highlight (in yellow), certain kinds of text strings in a MSWord file.
For example:
1) An italicized comma, followed by whitespace, and then a non-italicized text. Thus, for example:
The second comma in this sentence, which is italicized, should be highlighted by the desired macro. But the comma in this sentence should not be highlighted, because the entire sentence is in italics.
2) A bolded character (of any kind, even whitespace), both preceded and followed by non-bolded characters. Thus, for example:
This sentence ends in a bolded punctuation mark. That first period should be highlighted.
I know that first period might look normal, but it's not. It's bold.
3) Any word that is in SmallCaps, and is >4 letters long, but is not capitalized. I don't know how to do smallcaps in markdown... but imagine for a moment that the following text is in smallcaps in MSWord:
Imagine All of This Is in Small Caps. . . the Word "under" Should Be Highlighted Because It Is More Than Four Characters Long but is not Capitalized
Does anyone know whether this is possible? I know it's quite easy to find text-patterns using regular expressions, but adding changes in formatting to those patterns seems to be tricky.
run cmd,
cscript //Nologo regexp02.vbs
regexp02.vbs:
Dim objRegExp : Set objRegExp = CreateObject("VBScript.RegExp")
objRegExp.Global = True
Dim input
input="Imagine All of This Is in Small Caps. . . the Word under Should Be Highlighted Because It Is More Than Four Characters Long but is not Capitalized"
WScript.Echo input
WScript.Echo
Dim Pattern1 : Pattern1 = "\b[a-z]{5,}\s"
WScript.Echo "Pattern1 : " & Pattern1
WScript.Echo
objRegExp.Pattern = Pattern1
Set objMatches = objRegExp.Execute(input)
For i=0 To objMatches.Count-1
Set objMatch = objMatches.Item(i)
WScript.Echo objMatch.Value
Next
WScript.Echo
Dim Pattern2 : Pattern2 = "\b[A-Z]([a-z]{4,})\s"
WScript.Echo "Pattern2 : " & Pattern2
WScript.Echo
objRegExp.Pattern = Pattern2
Set objMatches = objRegExp.Execute(input)
For i=0 To objMatches.Count-1
Set objMatch = objMatches.Item(i)
WScript.Echo objMatch.Value
WScript.Echo Left(objMatch.Value, 1)
'TODO test bold sumbol Left(objMatch.Value, 1)
'
' TODO Highlight Code
'
Next
Output:
Imagine All of This Is in Small Caps. . . the Word under Should Be Highlighted Because It Is More Than Four Characters Long but is not Capitalized
Pattern1 : \b[a-z]{5,}\s
under
Pattern2 : \b[A-Z]([a-z]{4,})\s
Imagine
I
Small
S
Should
S
Highlighted
H
Because
B
Characters
C
Regex at VBA:
Open reference
Select COM-server Microsoft VBScript Regular Expressions 5.5
VBA code:
Dim objRegExp As New VBScript_RegExp_55.RegExp
objRegExp.IgnoreCase = False
objRegExp.Global = True
objRegExp.Pattern = Pattern1
Record macro
Press Ctrl+F, open search dialog
select font property
select font style
Press Find Next
Stop macro record, open VBA editor
Edit macro SearchItalic
Run macro SearchItalic
Search italic text:
Sub SearchItalic()
Selection.Find.ClearFormatting
Selection.Find.Font.Italic = True
With Selection.Find
.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
End Sub