Different space indentation for function parameters definition vim/neovim - neovim

i am using a 2 space tab in nvim, and i want to have a 4 space indentation for the parameters of a function,
the current behavior looks like this
func eventLoop(w *app.Window,
ops op.Ops,
th *material.Theme,
startButton widget.Clickable) {
for e := range w.Events() {
switch e := e.(type) {
case system.FrameEvent:
...
this is however how i want it to look
func eventLoop(w *app.Window,
ops op.Ops,
th *material.Theme,
startButton widget.Clickable) {
for e := range w.Events() {
switch e := e.(type) {
case system.FrameEvent:
...
I also want the editor to auto format the code in this way whenever i save the buffer.
this code is in go and i am using the coc extension.

Related

Swift + Markdown: plain text is changed after reading and writing to file

I'm using Apples own Markdown framework (https://github.com/apple/swift-markdown) and when I have a markdown file like the following:
## this is my heading 2
- this is some unordered list entry
- and another one
- and still anther one
- and an indented one
When I read and write without any changes:
let data = FileManager.default.contents(atPath: "myfile.md")!
var document = Document(parsing: String(data: data, encoding: .utf8)!)
let url = URL(filePath: "myfile.md")
try document.format(options: .init()).data(using: .utf8)!.write(to: url)
the content changes:
## this is my heading 2
- this is some unordered list entry
- and another one
- and still anther one
- and an indented one
i.e. a newline is printed after the heading and the indention of the unordered list is changed to two spaces instead of 4/tab. Is there a way to prevent/configure this?
No, you cannot configure either of these without creating your own fork of swift-markdown. For the extra newline, take a look at this code:
mutating public func visitUnorderedList(_ unorderedList: UnorderedList) {
if unorderedList.indexInParent > 0 && !(unorderedList.parent?.parent is ListItemContainer) {
ensurePrecedingNewlineCount(atLeast: 2)
}
descendInto(unorderedList)
}
mutating public func visitOrderedList(_ orderedList: OrderedList) {
if orderedList.indexInParent > 0 && !(orderedList.parent?.parent is ListItemContainer) {
ensurePrecedingNewlineCount(atLeast: 2)
}
descendInto(orderedList)
}
Observe the ensurePrecedingNewlineCount(atLeast: 2) call.
For the indentation of lists, take a look at this code from the swift-markdown repository:
} else if element is UnorderedList {
if unorderedListCount > 0 {
prefix += " "
}
unorderedListCount += 1
} else if element is OrderedList {
if orderedListCount > 0 {
prefix += " "
}
orderedListCount += 1
} else if !(element is ListItem),
let parentListItem = element.parent as? ListItem {
/*
Align contents with list item markers.
Example, unordered lists:
- First line
Second line, aligned.
Example, ordered lists:
1. First line
Second line, aligned.
1000. First line
Second line, aligned.
*/
if parentListItem.parent is UnorderedList {
// Unordered list markers are of fixed length.
prefix += " "
} else if let numeralPrefix = numeralPrefix(for: parentListItem) {
prefix += String(repeating: " ", count: numeralPrefix.count)
}
Note the hardcoded spaces to add to the prefix variable when elements are children of lists.
It's worth noting that at least the extra newline is required by the Markdown spec, and some parsers will not properly read the markdown unless there are two newlines between each logical "section" (paragraph, list, block quote, code block, etc.). I'm not sure whether the indentation is required by the spec, but that may also be why the team at Apple made that decision.
Also, one thing that may make this more understandable is that when you parse your markdown document, it represents the document in a tree structure like this:
// Document
// └─ Paragraph
// ├─ Text "This is a markup "
// ├─ Emphasis
// │ └─ Text "document"
// └─ Text "."
and so it has completely forgotten about your formatting. When you then write it back out, it generates Markdown text from this tree. So although you made no changes to the document in your code, it has been parsed and then the Markdown is regenerated, rather than keeping your original document in memory exactly as-is and modifying it on the fly.

VS Code, Prettier and Flow not working nicely together - what am I missing?

I'm using the prettier-vscode extension to format my js code, but flow annotations are formatted in a weird way with lots of extra space:
const handleResponse = async function < T > ( // <-- should be function<T>(
response,
url: string,
options: responseOptions = {}
): Promise < T | null > { // <-- should be Promise<T|null> {
or Promise<T | null> {
I've looked around in all the places I could think of to see what config I need to change to get this working, but I can't find anywhere that seems to affect the spacing in te above case.
There's also some weirdness going on with multi-line ternary statements, but I don't think that's related to flow:
const WORKORDERS_BASE_URL =
WORKORDERS_PORT === '80' ? // <-- I'd prefer if ? and : were on the next line
`http://${WORKORDERS_HOST}` : // <-- on same indentation level as above - I want one more
`http://${WORKORDERS_HOST}:${WORKORDERS_PORT}` // <-- same here, I want more indentation
The worst case of them all is this change, which happens when I format the document:
Promise<?T>
// becomes
Promise<<?T>
i.e. it actually breaks my code!
Where should I look for config that controls these things?

Intellij indentation format - method definition with/without braces (in scala)

I am having a minor annoyance with intellij indentation which I hope someone can help me with.
If I write a method declaration with braces, indenting works properly:
def function(a:A): B = { // When I hit return on this line...
// the cursor goes here (4 spaces in)
}
However, if I don't include braces, I don't get any indentation:
def function(a:A): B = // When I hit return on this line...
// the cursor goes here (0 spaces in)
To make matters stranger, if I already have some code on the first line before hitting return, I do get the correct spacing
Before:
def function(a:A): B = some.code.here //cursor is immediately after the = sign
After:
def function(a:A): B =
some.code.here // Correct indentation behavior.
So what I want to know is this: is there a setting which will result in an auto-indent if I hit return at the end of a method declaration without any code behind the cursor?
Thanks!

Remove diacritics using Go

How can I remove all diacritics from the given UTF8 encoded string using Go? e.g. transform the string "žůžo" => "zuzo". Is there a standard way?
You can use the libraries described in Text normalization in Go.
Here's an application of those libraries:
// Example derived from: http://blog.golang.org/normalization
package main
import (
"fmt"
"unicode"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
func isMn(r rune) bool {
return unicode.Is(unicode.Mn, r) // Mn: nonspacing marks
}
func main() {
t := transform.Chain(norm.NFD, transform.RemoveFunc(isMn), norm.NFC)
result, _, _ := transform.String(t, "žůžo")
fmt.Println(result)
}
To expand a bit on the existing answer:
The internet standard for comparing strings of different character sets is called "PRECIS" (Preparation, Enforcement, and Comparison of Internationalized Strings in Application Protocols) and is documented in RFC7564. There is also a Go implementation at golang.org/x/text/secure/precis.
None of the standard profiles will do what you want, but it would be fairly straight forward to define a new profile that did. You would want to apply Unicode Normalization Form D ("D" for "Decomposition", which means the accents will be split off and be their own combining character), and then remove any combining character as part of the additional mapping rule, then recompose with the normalization rule. Something like this:
package main
import (
"fmt"
"unicode"
"golang.org/x/text/secure/precis"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
func main() {
loosecompare := precis.NewIdentifier(
precis.AdditionalMapping(func() transform.Transformer {
return transform.Chain(norm.NFD, transform.RemoveFunc(func(r rune) bool {
return unicode.Is(unicode.Mn, r)
}))
}),
precis.Norm(norm.NFC), // This is the default; be explicit though.
)
p, _ := loosecompare.String("žůžo")
fmt.Println(p, loosecompare.Compare("žůžo", "zuzo"))
// Prints "zuzo true"
}
This lets you expand your comparison with more options later (eg. width mapping, case mapping, etc.)
It's also worth noting that removing accents is almost never what you actually want to do when comparing strings like this, however, without knowing your use case I can't actually make that assertion about your project. To prevent the proliferation of precis profiles it's good to use one of the existing profiles where possible. Also note that no effort was made to optimize the example profile.
transform.RemoveFunc is deprecated.
Instead you can use the Remove function from runes package:
t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
result, _, _ := transform.String(t, "žůžo")
fmt.Println(result)
For anyone looking how to remove (or replace / flatten) Polish diacritics in Go, you may define a mapping for runes:
package main
import (
"fmt"
"golang.org/x/text/runes"
"golang.org/x/text/secure/precis"
"golang.org/x/text/transform"
"golang.org/x/text/unicode/norm"
)
func main() {
trans := transform.Chain(
norm.NFD,
precis.UsernameCaseMapped.NewTransformer(),
runes.Map(func(r rune) rune {
switch r {
case 'ą':
return 'a'
case 'ć':
return 'c'
case 'ę':
return 'e'
case 'ł':
return 'l'
case 'ń':
return 'n'
case 'ó':
return 'o'
case 'ś':
return 's'
case 'ż':
return 'z'
case 'ź':
return 'z'
}
return r
}),
norm.NFC,
)
result, _, _ := transform.String(trans, "ŻóŁć")
fmt.Println(result)
}
On Go Playground: https://play.golang.org/p/3ulPnOd3L91

Wordpress TinyMCE: Switch Views

In order to complete my Wordpress Plugin I want to make tinyMCE to switch between a custom Tag (Some|data|here) and a corresponding Image Display in the WYSIWYG-View.
The event should be triggered on load, safe, autosave, switch view etc. Threre are 4 different events defined, but none of them works as expected.
onBeforeSetContent
onGetContent
onPostProcess
onLoadContent
.
ed.onPostProcess.add(function(ed, o) {
if (o.set){
o.content = t._htmlToWysiwyg(o.content, url);
}
if (o.get){
o.content = t._wysiwygToHtml(o.content, t);
}
});
Anyon know the right way?
I do not know what you expect the 4 different events will do (?), but i can see some problems in your code.
1. The object o does not contain fields get and set - so o.get and o.set will never be true! Thus your code will never be called.
2. You are using the variable url, but this one is not defined here.
Working example: You may try to paste a string containing "a" into the editor. Use the following:
ed.onPostProcess.add(function(ed, o) {
//console.log('o:', o);
o.content = o.content.replace(/a/g, "A");
});
You should see that all lower 'a's get replaced by 'A's.