From 534853adb6080762211be61206749b2145cca34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Qian=20Qian=20=22Cubik=22=E2=80=8E?= Date: Thu, 11 Apr 2024 01:49:54 -0400 Subject: [PATCH] feat: Define comment strings for each language (#65) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: Add basic implementation for language comment definition and Swift definition Signed-off-by: Qian Qian "Cubik"‎ * refactor: Improved documentation comment strings definition Signed-off-by: Qian Qian "Cubik"‎ * feat: Add comment definition for `agda`, `bash`, `c`, and `cpp` Signed-off-by: Qian Qian "Cubik"‎ * feat: Add some other language comments' definition * style: Fix some SwiftLint issue Signed-off-by: Qian Qian "Cubik"‎ * feat: Add Java documentation string Signed-off-by: Qian Qian "Cubik"‎ * Disable File Length Check for Code Language Definition Signed-off-by: Qian Qian "Cubik"‎ --------- Signed-off-by: Qian Qian "Cubik"‎ Co-authored-by: Evren Sen <146845123+evrsen@users.noreply.github.com> --- .../CodeLanguage+Definitions.swift | 158 ++++++++++++++---- Sources/CodeEditLanguages/CodeLanguage.swift | 53 ++++++ 2 files changed, 183 insertions(+), 28 deletions(-) diff --git a/Sources/CodeEditLanguages/CodeLanguage+Definitions.swift b/Sources/CodeEditLanguages/CodeLanguage+Definitions.swift index a16d321..f16eba8 100644 --- a/Sources/CodeEditLanguages/CodeLanguage+Definitions.swift +++ b/Sources/CodeEditLanguages/CodeLanguage+Definitions.swift @@ -1,3 +1,5 @@ +// swiftlint:disable file_length + // // CodeLanguage+Definitions.swift // @@ -58,21 +60,27 @@ public extension CodeLanguage { static let agda: CodeLanguage = .init( id: .agda, tsName: "agda", - extensions: ["agda"] + extensions: ["agda"], + lineCommentString: "--", + rangeCommentStrings: ("{-", "-}") ) /// A language structure for `Bash` static let bash: CodeLanguage = .init( id: .bash, tsName: "bash", - extensions: ["sh", "bash"] + extensions: ["sh", "bash"], + lineCommentString: "#", + rangeCommentStrings: (":'", "'") ) /// A language structure for `C` static let c: CodeLanguage = .init( id: .c, tsName: "c", - extensions: ["c", "h"] + extensions: ["c", "h"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `C++` @@ -80,6 +88,9 @@ public extension CodeLanguage { id: .cpp, tsName: "cpp", extensions: ["cc", "cpp", "c++", "hpp", "h"], + lineCommentString: CodeLanguage.c.lineCommentString, + rangeCommentStrings: CodeLanguage.c.rangeCommentStrings, + documentationCommentStrings: [.pair(("/**", "*/"))], parentURL: CodeLanguage.c.queryURL, highlights: ["injections"] ) @@ -88,28 +99,36 @@ public extension CodeLanguage { static let cSharp: CodeLanguage = .init( id: .cSharp, tsName: "c-sharp", - extensions: ["cs"] + extensions: ["cs"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `CSS` static let css: CodeLanguage = .init( id: .css, tsName: "css", - extensions: ["css"] + extensions: ["css"], + lineCommentString: "", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `Dart` static let dart: CodeLanguage = .init( id: .dart, tsName: "dart", - extensions: ["dart"] + extensions: ["dart"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `Dockerfile` static let dockerfile: CodeLanguage = .init( id: .dockerfile, tsName: "dockerfile", - extensions: ["Dockerfile"] + extensions: ["Dockerfile"], + lineCommentString: "#", + rangeCommentStrings: ("", "") ) /// A language structure for `Elixir` @@ -117,6 +136,9 @@ public extension CodeLanguage { id: .elixir, tsName: "elixir", extensions: ["ex", "exs"], + lineCommentString: "#", + rangeCommentStrings: ("", ""), + documentationCommentStrings: [.pair(("\"\"\"", "\"\"\""))], highlights: ["injections"] ) @@ -124,21 +146,27 @@ public extension CodeLanguage { static let go: CodeLanguage = .init( id: .go, tsName: "go", - extensions: ["go"] + extensions: ["go"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `GoMod` static let goMod: CodeLanguage = .init( id: .goMod, tsName: "go-mod", - extensions: ["mod"] + extensions: ["mod"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `Haskell` static let haskell: CodeLanguage = .init( id: .haskell, tsName: "haskell", - extensions: ["hs"] + extensions: ["hs"], + lineCommentString: "--", + rangeCommentStrings: ("{-", "-}") ) /// A language structure for `HTML` @@ -146,6 +174,8 @@ public extension CodeLanguage { id: .html, tsName: "html", extensions: ["html", "htm", "shtml"], + lineCommentString: "", + rangeCommentStrings: (""), highlights: ["injections"] ) @@ -153,7 +183,10 @@ public extension CodeLanguage { static let java: CodeLanguage = .init( id: .java, tsName: "java", - extensions: ["java", "jav"] + extensions: ["java", "jav"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/"), + documentationCommentStrings: [.pair(("/**", "*/"))] ) /// A language structure for `JavaScript` @@ -161,6 +194,9 @@ public extension CodeLanguage { id: .javascript, tsName: "javascript", extensions: ["js", "cjs", "mjs"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/"), + documentationCommentStrings: [.pair(("/**", "*/"))], highlights: ["injections"], additionalIdentifiers: ["node", "deno"] ) @@ -169,14 +205,18 @@ public extension CodeLanguage { static let jsdoc: CodeLanguage = .init( id: .jsdoc, tsName: "jsdoc", - extensions: [] + extensions: [], + lineCommentString: "", + rangeCommentStrings: ("/**", "*/") ) /// A language structure for `JSON` static let json: CodeLanguage = .init( id: .json, tsName: "json", - extensions: ["json"] + extensions: ["json"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `JSX` @@ -184,6 +224,8 @@ public extension CodeLanguage { id: .jsx, tsName: "javascript", extensions: ["jsx"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/"), highlights: ["highlights-jsx", "injections"] ) @@ -191,14 +233,18 @@ public extension CodeLanguage { static let julia: CodeLanguage = .init( id: .julia, tsName: "julia", - extensions: ["jl"] + extensions: ["jl"], + lineCommentString: "#", + rangeCommentStrings: ("#=", "=#") ) /// A language structure for `Kotlin` static let kotlin: CodeLanguage = .init( id: .kotlin, tsName: "kotlin", - extensions: ["kt", "kts"] + extensions: ["kt", "kts"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `Lua` @@ -206,6 +252,8 @@ public extension CodeLanguage { id: .lua, tsName: "lua", extensions: ["lua"], + lineCommentString: "--", + rangeCommentStrings: ("-[[", "]]--"), highlights: ["injections"] ) @@ -214,6 +262,8 @@ public extension CodeLanguage { id: .markdown, tsName: "markdown", extensions: ["md", "mkd", "mkdn", "mdwn", "mdown", "markdown"], + lineCommentString: "[comment]: #", + rangeCommentStrings: ("", ""), highlights: ["injections"] ) @@ -222,6 +272,8 @@ public extension CodeLanguage { id: .markdownInline, tsName: "markdown-inline", extensions: [], + lineCommentString: "", + rangeCommentStrings: ("", ""), highlights: ["injections"] ) @@ -229,28 +281,37 @@ public extension CodeLanguage { static let objc: CodeLanguage = .init( id: .objc, tsName: "objc", - extensions: ["m", "h"] + extensions: ["m", "h"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `OCaml` static let ocaml: CodeLanguage = .init( id: .ocaml, tsName: "ocaml", - extensions: ["ml"] + extensions: ["ml"], + lineCommentString: "", + rangeCommentStrings: ("(*", "*)"), + documentationCommentStrings: [.pair(("(**", "*)"))] ) /// A language structure for `OCaml Interface` static let ocamlInterface: CodeLanguage = .init( id: .ocamlInterface, tsName: "ocaml", - extensions: ["mli"] + extensions: ["mli"], + lineCommentString: "", + rangeCommentStrings: ("", "") ) /// A language structure for `Perl` static let perl: CodeLanguage = .init( id: .perl, tsName: "perl", - extensions: ["pl", "pm"] + extensions: ["pl", "pm"], + lineCommentString: "#", + rangeCommentStrings: ("=pod", "=cut") ) /// A language structure for `PHP` @@ -258,6 +319,8 @@ public extension CodeLanguage { id: .php, tsName: "php", extensions: ["php"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/"), highlights: ["injections"] ) @@ -266,6 +329,9 @@ public extension CodeLanguage { id: .python, tsName: "python", extensions: ["py"], + lineCommentString: "#", + rangeCommentStrings: ("", ""), + documentationCommentStrings: [.pair(("\"\"\"", "\"\"\""))], additionalIdentifiers: ["python2", "python3"] ) @@ -273,14 +339,18 @@ public extension CodeLanguage { static let regex: CodeLanguage = .init( id: .regex, tsName: "regex", - extensions: [] + extensions: [], + lineCommentString: "", + rangeCommentStrings: ("", "") ) /// A language structure for `Ruby` static let ruby: CodeLanguage = .init( id: .ruby, tsName: "ruby", - extensions: ["rb"] + extensions: ["rb"], + lineCommentString: "#", + rangeCommentStrings: ("=begin", "=end") ) /// A language structure for `Rust` @@ -288,6 +358,14 @@ public extension CodeLanguage { id: .rust, tsName: "rust", extensions: ["rs"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/"), + documentationCommentStrings: [ + .single("///"), + .single("//!"), + .pair(("/**", "*/")), + .pair(("/*!", "*/")) + ], highlights: ["injections"] ) @@ -295,28 +373,37 @@ public extension CodeLanguage { static let scala: CodeLanguage = .init( id: .scala, tsName: "scala", - extensions: ["scala", "sc"] + extensions: ["scala", "sc"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `SQL` static let sql: CodeLanguage = .init( id: .sql, tsName: "sql", - extensions: ["sql"] + extensions: ["sql"], + lineCommentString: "--", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `Swift` static let swift: CodeLanguage = .init( id: .swift, tsName: "swift", - extensions: ["swift"] + extensions: ["swift"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/"), + documentationCommentStrings: [.single("///"), .pair(("/**", "*/"))] ) /// A language structure for `TOML` static let toml: CodeLanguage = .init( id: .toml, tsName: "toml", - extensions: ["toml"] + extensions: ["toml"], + lineCommentString: "#", + rangeCommentStrings: ("", "") ) /// A language structure for `TSX` @@ -324,6 +411,8 @@ public extension CodeLanguage { id: .tsx, tsName: "typescript", extensions: ["tsx"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/"), parentURL: CodeLanguage.jsx.queryURL ) @@ -332,6 +421,8 @@ public extension CodeLanguage { id: .typescript, tsName: "typescript", extensions: ["ts", "cts", "mts"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/"), parentURL: CodeLanguage.javascript.queryURL ) @@ -339,14 +430,18 @@ public extension CodeLanguage { static let verilog: CodeLanguage = .init( id: .verilog, tsName: "verilog", - extensions: ["v"] + extensions: ["v"], + lineCommentString: "//", + rangeCommentStrings: ("/*", "*/") ) /// A language structure for `YAML` static let yaml: CodeLanguage = .init( id: .yaml, tsName: "yaml", - extensions: ["yml", "yaml"] + extensions: ["yml", "yaml"], + lineCommentString: "#", + rangeCommentStrings: ("", "") ) /// A language structure for `Zig` @@ -354,6 +449,9 @@ public extension CodeLanguage { id: .zig, tsName: "zig", extensions: ["zig"], + lineCommentString: "//", + rangeCommentStrings: ("", ""), + documentationCommentStrings: [.single("///"), .single("//!")], highlights: ["injections"] ) @@ -361,6 +459,10 @@ public extension CodeLanguage { static let `default`: CodeLanguage = .init( id: .plainText, tsName: "PlainText", - extensions: ["txt"] + extensions: ["txt"], + lineCommentString: "", + rangeCommentStrings: ("", "") ) } + +// swiftlint:enable file_length diff --git a/Sources/CodeEditLanguages/CodeLanguage.swift b/Sources/CodeEditLanguages/CodeLanguage.swift index a109b55..07d8b6c 100644 --- a/Sources/CodeEditLanguages/CodeLanguage.swift +++ b/Sources/CodeEditLanguages/CodeLanguage.swift @@ -17,6 +17,9 @@ public struct CodeLanguage { id: TreeSitterLanguage, tsName: String, extensions: Set, + lineCommentString: String, + rangeCommentStrings: (String, String), + documentationCommentStrings: Set = [], parentURL: URL? = nil, highlights: Set? = nil, additionalIdentifiers: Set = [] @@ -24,6 +27,9 @@ public struct CodeLanguage { self.id = id self.tsName = tsName self.extensions = extensions + self.lineCommentString = lineCommentString + self.rangeCommentStrings = rangeCommentStrings + self.documentationCommentStrings = documentationCommentStrings self.parentQueryURL = parentURL self.additionalHighlights = highlights self.additionalIdentifiers = additionalIdentifiers @@ -41,6 +47,15 @@ public struct CodeLanguage { /// (e.g `Dockerfile`, `Makefile`) public let extensions: Set + /// The leading string of a comment line + public let lineCommentString: String + + /// The leading and trailing string of a multi-line comment + public let rangeCommentStrings: (String, String) + + /// The leading (and trailing, if there is one) string of a documentation comment + public let documentationCommentStrings: Set + /// The query URL of a language this language inherits from. (e.g.: C for C++) public let parentQueryURL: URL? @@ -161,7 +176,45 @@ public struct CodeLanguage { } extension CodeLanguage: Hashable { + public static func == (lhs: CodeLanguage, rhs: CodeLanguage) -> Bool { + return lhs.id == rhs.id + } + public func hash(into hasher: inout Hasher) { hasher.combine(id) } } + +public enum DocumentationComments: Hashable { + public static func == (lhs: DocumentationComments, rhs: DocumentationComments) -> Bool { + switch lhs { + case .single(let lhsString): + switch rhs { + case .single(let rhsString): + return lhsString == rhsString + case .pair: + return false + } + case .pair(let lhsPair): + switch rhs { + case .single: + return false + case .pair(let rhsPair): + return lhsPair.0 == rhsPair.0 && lhsPair.1 == rhsPair.1 + } + } + } + + public func hash(into hasher: inout Hasher) { + switch self { + case .single(let string): + hasher.combine(string) + case .pair(let pair): + hasher.combine(pair.0) + hasher.combine(pair.1) + } + } + + case single(String) + case pair((String, String)) +}