Snippet for title in restructured text in vs code - visual-studio-code

In restructured text, titles are written with equal number of nonalphanumeric 7-bit ASCII
character as the title text. The underline and overline if both used, should be equal and at least as long as title text. From the official docs:
Titles are underlined (or over-
and underlined) with a printing
nonalphanumeric 7-bit ASCII
character. Recommended choices
are "= - ` : ' " ~ ^ _ * + # < >".
The underline/overline must be at
least as long as the title text.
Example of a title
=========================================================
Main titles are written using equals signs over and under
=========================================================
I want to create a VS Code snippet for this. What I could do was only this,
"Title RST": {
"prefix": "title",
"body": [
"="
"$1"
"=\n"
"$0"
],
"description": "Title for restructured text"
}
Is there a way to know the length of the text that will be typed, and correspondingly insert same number of overline and underline =.
In yasnippet in emacs, they do it as:
${1:$(make-string (string-width yas-text) ?\=)}
${1:Title}
${1:$(make-string (string-width yas-text) ?\=)}
$0
Any help how to implement such snippet in VS code? I looked under snippets in restructured text extension for VS Code here but could not find that suits my needs.

"Title RST": {
"prefix": "title",
"body": [
"${1/./=/g}",
"$1",
"${1/./=/g}",
"$0"
],
"description": "Title for restructured text"
},
The transforms ${1/./=/g} just replace every character in your text $1 with a = in the line above and below your text.
You need commas at the end of your snippet entries and there is no need for the newline as another line in the snippet body is already a newline.
When you type your text hit Tab and the transform will be completed.
You asked if was possible to get the over/underlines to show as =s immediately upon typing your title text. But that isn't possible with vscode snippets, a transform is required and that won't happen until the Tab.
It can be done with HyperSnips version (a little more trouble to set up than plain vscode snippets, but not much):
snippet title "Title" A
``rv = '='.repeat(t[0].length)``
$1
``rv = '='.repeat(t[0].length)``
endsnippet

Related

Formating Visual Studio Column Jump

I'd like to edit my Visual Studio Code.
When I write my symbols on my keyboard to VSC, they'll get written Column by Column on the current Row.
\ Line of Code + Column.
(Ln , Col )
Extensions: "Select By"
= to jump to a certain column.
However, when i like to jump to 'column 50', it will only jump to the >End of Line.
// Example
// Goal is to get a Hotkey to Jump to Column 50
// Activated Hotkey*
cout << "Hello World:" << endl; // If i use my Hotkey i get to the Column 30, not 50. Picture is enhanced*
/* The end of line is here: 30. */
Question: How can I achieve my Jump
via the given Extension? (Any easy solution would be helpful).
I don't like to use the Ln, Col in Vsc, because clicking on the UI is for me over the time to slow.
I'd like to use f.e. Cmd + M to get to my "middle", So for me column 50.
Hence, i don't like to go to Middle of my current Screenformat.
Additional prefered Question: One way how i like to solve the issue is in C64 Style.
Is there any way to fill out the Visual Studio Code Screen by empty chars?
So as an Preset.
I know, that i can just copy & Paste. Or write an empty script. But that doesn't seem convenient inside the Configuration. I tryed to find that now unsucessfully since 2 weeks..
The reason is for me, that i currently try to code more cleaner.
And to achieve that (I have read many Clean Code Books during school and Github), i just like to try this time an historical approach by setting my own column points/ marks. (Basic C64-menu wise)
Thank you in advance!
Sincerely
Edit: I have enhanced beforehand.
Edit2: I have tryed the Find and Transform Extension pointed by Mark. Thank you for your time!
When i press the given Hotkey, It does marks the previous chars. but it doesn't start at column 50.
It marks at end of line.
I only like to move the cursor to column 50 and surpress the end of line.
does this key binding work
{
"key": "ctrl+i ctrl+m", // or any other key binding
"when": "editorTextFocus",
"command": "moveby.calculation",
"args": {
"charNrEx": "50"
}
}
Or use Ctrl+G and type lineNr:charNr
At the risk of not fully understanding the question, if what you want to do is pad any line to the 50th column with spaces, you can try this approach.
Install the extension Find and Transform
Make this keybinding (or it could be a command):
{
"key": "alt+q", // whatever keybinding you want
"command": "findInCurrentFile",
"args": {
// this will select the current line, cursor can start anywhere on line
"preCommands": ["cursorEnd", "cursorHomeSelect", "cursorHomeSelect"],
"replace": [
"$${",
"const str = `${TM_CURRENT_LINE}`;",
"let currentLength = str.length;", // current line length
"if (currentLength >= 49) return str;", // do nothing
// pad to whatever column you want
"else return `${str.padEnd(49, ' ')}`;", // pad with spaces
"}$$"
],
"restrictFind": "line", // only on lines with a cursor
"postCommands": ["cursorRight"] // just cancels the selection
}
}
It'll work on multiple lines too if each line has a cursor on it

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

How to auto-escape a special char in VS Code Snippets?

I want to write a snippet for Debugging in TYPO3.
This is my Snippet-Code in php.json file:
"TYPO3 Extbase DebuggerUtility": {
"prefix": "ee",
"body": [
"\\TYPO3\\CMS\\Extbase\\Utility\\DebuggerUtility::var_dump($1,'$1');",
"$0"
],
"description": "TYPO3 Extbase DebuggerUtility"
},
If I want to debug something liket this : $this->settings['key'] I get this code:
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->settings['key'],'$this->settings['key']');
But it should looks like this
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->settings['key'],'$this->settings[\'key\']');
With escaped ' in the second part of that snippet.
EDIT
Thank you, but I think you missunderstood the question.
I don't want to escape a static character. I want to use the snippet and when I type the first $1-content it should be $this->settings['someKey'] but the second $1 (which is near the same) should automatically escape the ' chars I write, that I don't do this manually by hand.
So if i type '
first $1: ' second $1: \' that my Debug looks like this
Debug:
$this->settings['someKey']
contentOfsomeKey
I I don't escape the ' signs inside the "title of the debug" it breaks the string because ' wraps the debug-title.
In other words: I want to escape the content of the second $1 variable not the variable or the '-wrap in the snippet.
I hope I could clarify my issue.
If you want escape characters \ in your output you need to insert escaped escape characters: \\ this should result in single escape characters.
You might need an additional escape character if the following character needs an additional escaping: one backslash before quote \' = \\+ \' = \\\'
`

powershell edit powerpoint slide notes

I am trying to use PowerShell to pro-grammatically update notes in PowerPoint slide notes. Being able to do this will save tremendous amounts of time. The code below allows me to edit the notes field with PowerShell but it messes up the format each time.
$PowerpointFile = "C:\Users\username\Documents\test.pptx"
$Powerpoint = New-Object -ComObject powerpoint.application
$ppt = $Powerpoint.presentations.open($PowerpointFile, 2, $True, $False)
foreach($slide in $ppt.slides){
if($slide.NotesPage.Shapes[2].TextFrame.TextRange.Text -match "string"){
$slide.NotesPage.Shapes[2].TextFrame.TextRange.Text = $slide.NotesPage.Shapes[2].TextFrame.TextRange.Text -replace "string","stringreplaced"
}
}
Sleep -Seconds 3
$ppt.Save()
$Powerpoint.Quit()
For example, right now it will iterate through each slide's notes and update the word string to stringreplaced but then the entire notes text becomes bold. In my notes I have a single word at the top of the notes that is bold and then text below it. For example, a note on a slide my look like this:
Note Title
Help me with this string.
After PowerShell updates the notes field it saves it to a new .pptx file but the note now looks like this:
Note Title
Help me with this stringreplaced.
Any ideas on how to update slide notes without messing up any formatting found in the notes? It only messes up formatting for slides the script updates.
When you change the entire text content of a textrange in PPT, as your code's doing, the changed textrange will pick up the formatting of the first character in the range. I'm not sure how you'd do this in PowerShell, but here's an example in PPT VBA that demonstrates the same problem and shows how to use PPT's own Replace method instead to solve the problem:
Sub ExampleTextReplace()
' Assumes two shapes with text on Slide 1 of the current presentation
' Each has the text "This is some sample text"
' The first character of each is bolded
' Demonstrates the difference between different methods of replacing text
' within a string
Dim oSh As Shape
' First shape: change the text
Set oSh = ActivePresentation.Slides(1).Shapes(1)
With oSh.TextFrame.TextRange
.Text = Replace(.Text, "sample text", "example text")
End With
' Result: the entire text string is bolded
' Second shape: Use PowerPoint's Replace method instead
Set oSh = ActivePresentation.Slides(1).Shapes(2)
With oSh.TextFrame.TextRange
.Replace "sample text", "example text"
End With
' Result: only the first character of the text is bolded
' as it was originally
End Sub

How to mirror statements in Notepad++

I have a file with hundreds of these kind of statements:
If Description = "Approach light" Then Obstakelcode = "AL"
If Description = "Common mast" Then Obstakelcode = "CoM"
etc.
With a Notepad++ macro I tried to 'mirror' these statements in:
If Obstakelcode = "AL" Then Description = "Approach light"
If Obstakelcode = "CoM" Then Description = "Common mast"
etc.
However, I failed. Can anybody tell me if this can be done easily with Notepad++?
On the Notepad++ Find/Replace screen, with Regular expression selected, please type
Find what
If\s(Description\s+=\s+\"[^\"]+\")\s+Then\s(Obstakelcode\s=\s\"[^\"]+\")
Replace with
If \2 Then \1
The expression between If and Then is captured as group \1 and the expression between Then up to the closing " is captured as \2. The Replace command mirrors it according to your requirement.