Merge pull request 'new qmlfmt script and test application to main.qml' (#280) from dan/ui:qmlfmt into master
the build was successful
Details
the build was successful
Details
This commit is contained in:
commit
73d8951d6e
|
@ -0,0 +1,86 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
indent = " "
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) < 2 {
|
||||
log.Fatal("Required argument(s): filename(s)")
|
||||
}
|
||||
|
||||
for _, filename := range os.Args[1:] {
|
||||
processFile(filename)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func processFile(filename string) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not read file %v: %v\n", filename, err)
|
||||
}
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
var lines []string
|
||||
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
file.Close()
|
||||
|
||||
file, err = os.Create(filename)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
log.Fatalf("Could not write to file %v: %v\n", filename, err)
|
||||
}
|
||||
|
||||
indentCount := 0
|
||||
inMultiLineComment := false
|
||||
|
||||
for _, line := range lines {
|
||||
|
||||
singleCommentPos := strings.Index(line, "//")
|
||||
multiLineCommentStartPos := strings.Index(line, "/*")
|
||||
multiLineCommentEndPos := strings.Index(line, "*/")
|
||||
|
||||
closePos := strings.Index(line, "}")
|
||||
if !inMultiLineComment && closePos > -1 && (singleCommentPos == -1 || closePos < singleCommentPos) &&
|
||||
(multiLineCommentStartPos == -1 || closePos < multiLineCommentStartPos) &&
|
||||
(multiLineCommentEndPos == -1 || closePos > multiLineCommentEndPos) {
|
||||
indentCount--
|
||||
}
|
||||
|
||||
trimedLine := strings.Trim(line, " \t")
|
||||
if trimedLine == "" {
|
||||
file.Write([]byte("\n"))
|
||||
} else {
|
||||
file.Write([]byte(strings.Repeat(indent, indentCount) + trimedLine + "\n"))
|
||||
}
|
||||
|
||||
openPos := strings.Index(line, "{")
|
||||
if !inMultiLineComment && openPos > -1 && (singleCommentPos == -1 || openPos < singleCommentPos) &&
|
||||
(multiLineCommentStartPos == -1 || openPos < multiLineCommentStartPos) &&
|
||||
(multiLineCommentEndPos == -1 || openPos > multiLineCommentEndPos) {
|
||||
indentCount++
|
||||
}
|
||||
|
||||
if multiLineCommentStartPos > -1 {
|
||||
inMultiLineComment = true
|
||||
}
|
||||
|
||||
if multiLineCommentEndPos > -1 {
|
||||
inMultiLineComment = false
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
#!/bin/sh
|
||||
|
||||
# go get cwtch.im/ui/cmd/qmlfmt
|
||||
|
||||
cd qml
|
||||
find -iname "*.qml" | xargs qmlfmt
|
||||
|
||||
cd ..
|
Reference in New Issue