mchakravarty / CodeEditorView

SwiftUI code editor view for iOS, visionOS, and macOS
Apache License 2.0
695 stars 62 forks source link

Add SQLite language configuration #116

Open barnettben opened 1 week ago

barnettben commented 1 week ago

As suggested in #112 (here), this ticket can be used to track progress of an SQL/SQLite language configuration.

I will hopefully have a go over the next week or so.

mchakravarty commented 1 week ago

@ericzakariasson I took the liberty to copy your code snippet into this issue for easier reference. Thank you — it looks fine to me.

I propose that we wait for @barnettben's SQL configuration, and then, we can have a look together, to see whether we can share some definitions or whether they need to be entirely separate configurations.

import LanguageSupport
import Foundation
import RegexBuilder

private let postgresReservedIds = [
    "ALL", "ANALYSE", "ANALYZE", "AND", "ANY", "ARRAY", "AS", "ASC", "ASYMMETRIC",
    "AUTHORIZATION", "BINARY", "BOTH", "CASE", "CAST", "CHECK", "COLLATE", "COLLATION",
    "COLUMN", "CONCURRENTLY", "CONSTRAINT", "CREATE", "CROSS", "CURRENT_CATALOG",
    "CURRENT_DATE", "CURRENT_ROLE", "CURRENT_SCHEMA", "CURRENT_TIME", "CURRENT_TIMESTAMP",
    "CURRENT_USER", "DEFAULT", "DEFERRABLE", "DESC", "DISTINCT", "DO", "ELSE", "END",
    "EXCEPT", "FALSE", "FETCH", "FOR", "FOREIGN", "FREEZE", "FROM", "FULL", "GRANT",
    "GROUP", "HAVING", "ILIKE", "IN", "INITIALLY", "INNER", "INTERSECT", "INTO", "IS",
    "ISNULL", "JOIN", "LATERAL", "LEADING", "LEFT", "LIKE", "LIMIT", "LOCALTIME",
    "LOCALTIMESTAMP", "NATURAL", "NOT", "NOTNULL", "NULL", "OFFSET", "ON", "ONLY",
    "OR", "ORDER", "OUTER", "OVERLAPS", "PLACING", "PRIMARY", "REFERENCES", "RETURNING",
    "RIGHT", "SELECT", "SESSION_USER", "SIMILAR", "SOME", "SYMMETRIC", "TABLE", "THEN",
    "TO", "TRAILING", "TRUE", "UNION", "UNIQUE", "USER", "USING", "VARIADIC", "VERBOSE",
    "WHEN", "WHERE", "WINDOW", "WITH"
]

private let postgresReservedOperators = [
    "+", "-", "*", "/", "%", "=", "<>", "!=", "<", ">", "<=", ">=", "||",
    "<<", ">>", "&<", "&>", "<<|", "|>>", "&<|", "|&>",
    "->", "->>", "#>", "#>>", "@>", "<@", "?", "?|", "?&",
    "&&", "-|-", "~~", "~~*", "!~~", "!~~*", "@@@", "::", "."
]

extension LanguageConfiguration {

    /// Language configuration for PostgreSQL
    public static func postgres(_ languageService: LanguageService? = nil) -> LanguageConfiguration {
        // numeric types
        let numberRegex = /[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/

        // identifiers
        let identifierRegex = /[a-zA-Z_][a-zA-Z0-9_$]*|"[^"]+"/

        // operators
        let operatorRegex = /[+\-*\/<>=!|&%^~?#@:.]+/

        // standard quotes and dollar quoting
        let stringRegex = /'(?:[^']|'')*'|"(?:[^"]|"")*"|(?:\$[^$]*\$).*?/

        return LanguageConfiguration(
            name: "PostgreSQL",
            supportsSquareBrackets: true,
            supportsCurlyBrackets: false,
            stringRegex: stringRegex,
            characterRegex: nil,
            numberRegex: numberRegex,
            singleLineComment: "--",
            nestedComment: (open: "/*", close: "*/"),
            identifierRegex: identifierRegex,
            operatorRegex: operatorRegex,
            reservedIdentifiers: postgresReservedIds,
            reservedOperators: postgresReservedOperators,
            languageService: languageService
        )
    }
}
barnettben commented 5 days ago

Hi @mchakravarty and @ericzakariasson,

I've opened a draft pull request with a mostly-working implementation. I've hit a bit of a block with negative numbers, but will have another look sometime this week.

Draft PR: #117.

mchakravarty commented 4 days ago

@barnettben That's great — thank you!

I had a quick look and commented on the issue with negative numbers in the PR. I will try to have a closer look this week, but can't promise that as it is quite a busy week already. Otherwise, I'll have a look next week.