๐Ÿš€ Grammar-Aware Code Formatter: Structure through separation (supports Go, JavaScript, TypeScript, JSX, and TSX)
go formatter code-formatter javascript typescript jsx tsx

refactor: Extract line pattern detection to patterns.go

fuwn.net a1798d9b bc920d7e

verified
+83 -81
-81
formatter.go
··· 6 6 "go/parser" 7 7 "go/token" 8 8 "reflect" 9 - "regexp" 10 9 "strings" 11 10 ) 12 - 13 - var ( 14 - closingBracePattern = regexp.MustCompile(`^\s*[\}\)]`) 15 - openingBracePattern = regexp.MustCompile(`[\{\(]\s*$`) 16 - caseLabelPattern = regexp.MustCompile(`^\s*(case\s|default\s*:)|(^\s+.*:\s*$)`) 17 - ) 18 - 19 - func isCommentOnly(sourceLine string) bool { 20 - for characterIndex := range len(sourceLine) { 21 - character := sourceLine[characterIndex] 22 - 23 - if character == ' ' || character == '\t' { 24 - continue 25 - } 26 - 27 - return len(sourceLine) > characterIndex+1 && sourceLine[characterIndex] == '/' && sourceLine[characterIndex+1] == '/' 28 - } 29 - 30 - return false 31 - } 32 - 33 - func isPackageLine(trimmedLine string) bool { 34 - return len(trimmedLine) > 8 && trimmedLine[:8] == "package " 35 - } 36 - 37 - func countRawStringDelimiters(sourceLine string) int { 38 - delimiterCount := 0 39 - insideDoubleQuotedString := false 40 - insideCharacterLiteral := false 41 - 42 - for characterIndex := 0; characterIndex < len(sourceLine); characterIndex++ { 43 - character := sourceLine[characterIndex] 44 - 45 - if insideCharacterLiteral { 46 - if character == '\\' && characterIndex+1 < len(sourceLine) { 47 - characterIndex++ 48 - 49 - continue 50 - } 51 - 52 - if character == '\'' { 53 - insideCharacterLiteral = false 54 - } 55 - 56 - continue 57 - } 58 - 59 - if insideDoubleQuotedString { 60 - if character == '\\' && characterIndex+1 < len(sourceLine) { 61 - characterIndex++ 62 - 63 - continue 64 - } 65 - 66 - if character == '"' { 67 - insideDoubleQuotedString = false 68 - } 69 - 70 - continue 71 - } 72 - 73 - if character == '\'' { 74 - insideCharacterLiteral = true 75 - 76 - continue 77 - } 78 - 79 - if character == '"' { 80 - insideDoubleQuotedString = true 81 - 82 - continue 83 - } 84 - 85 - if character == '`' { 86 - delimiterCount++ 87 - } 88 - } 89 - 90 - return delimiterCount 91 - } 92 11 93 12 type CommentMode int 94 13
+83
patterns.go
··· 1 + package main 2 + 3 + import "regexp" 4 + 5 + var ( 6 + closingBracePattern = regexp.MustCompile(`^\s*[\}\)]`) 7 + openingBracePattern = regexp.MustCompile(`[\{\(]\s*$`) 8 + caseLabelPattern = regexp.MustCompile(`^\s*(case\s|default\s*:)|(^\s+.*:\s*$)`) 9 + ) 10 + 11 + func isCommentOnly(sourceLine string) bool { 12 + for characterIndex := range len(sourceLine) { 13 + character := sourceLine[characterIndex] 14 + 15 + if character == ' ' || character == '\t' { 16 + continue 17 + } 18 + 19 + return len(sourceLine) > characterIndex+1 && sourceLine[characterIndex] == '/' && sourceLine[characterIndex+1] == '/' 20 + } 21 + 22 + return false 23 + } 24 + 25 + func isPackageLine(trimmedLine string) bool { 26 + return len(trimmedLine) > 8 && trimmedLine[:8] == "package " 27 + } 28 + 29 + func countRawStringDelimiters(sourceLine string) int { 30 + delimiterCount := 0 31 + insideDoubleQuotedString := false 32 + insideCharacterLiteral := false 33 + 34 + for characterIndex := 0; characterIndex < len(sourceLine); characterIndex++ { 35 + character := sourceLine[characterIndex] 36 + 37 + if insideCharacterLiteral { 38 + if character == '\\' && characterIndex+1 < len(sourceLine) { 39 + characterIndex++ 40 + 41 + continue 42 + } 43 + 44 + if character == '\'' { 45 + insideCharacterLiteral = false 46 + } 47 + 48 + continue 49 + } 50 + 51 + if insideDoubleQuotedString { 52 + if character == '\\' && characterIndex+1 < len(sourceLine) { 53 + characterIndex++ 54 + 55 + continue 56 + } 57 + 58 + if character == '"' { 59 + insideDoubleQuotedString = false 60 + } 61 + 62 + continue 63 + } 64 + 65 + if character == '\'' { 66 + insideCharacterLiteral = true 67 + 68 + continue 69 + } 70 + 71 + if character == '"' { 72 + insideDoubleQuotedString = true 73 + 74 + continue 75 + } 76 + 77 + if character == '`' { 78 + delimiterCount++ 79 + } 80 + } 81 + 82 + return delimiterCount 83 + }