class_name RuleSpacing const SYMBOLS = [ r"\*\*=", r"\*\*", "<<=", ">>=", "<<", ">>", "==", "!=", ">=", "<=", "&&", r"\|\|", r"\+=", "-=", r"\*=", "/=", "%=", "&=", r"\^=", r"\|=", "~=", ":=", "->", r"&", r"\|", r"\^", "-", r"\+", "/", r"\*", ">", "<", "-", "%", "=", ":", ",", ]; const KEYWORDS = [ "and", "is", "or", "not", ] static func apply(code: String) -> String: var string_regex = RegEx.new() string_regex.compile(r'(? String: var indent_regex = RegEx.create_from_string(r"^\s{4}") var new_code = indent_regex.sub(code, "\t", true) while (code != new_code): code = new_code new_code = indent_regex.sub(code, "\t", true) var symbols_regex = "(" + ") | (".join(SYMBOLS) + ")" symbols_regex = " * ?(" + symbols_regex + ") * " var symbols_operator_regex = RegEx.create_from_string(symbols_regex) code = symbols_operator_regex.sub(code, " $1 ", true) # ": =" => ":=" code = RegEx.create_from_string(r": *=").sub(code, ":=", true) # "a(" => "a (" code = RegEx.create_from_string(r"(?<=[\w\)\]]) *([\(:,])(?!=)").sub(code, "$1", true) # "( a" => "(a" code = RegEx.create_from_string(r"([\(\{}]) *").sub(code, "$1", true) # "a )" => "a)" code = RegEx.create_from_string(r" *([\)\}])").sub(code, "$1", true) # "if(" => "if (" code = RegEx.create_from_string(r"\b(if|for|while|switch|match)\(").sub(code, "$1 (", true) var keywoisrd_regex = r"|".join(KEYWORDS) var keyword_operator_regex = RegEx.create_from_string(r"(?<=[ \)\]])(" + keywoisrd_regex + r")(?=[ \(\[])") code = keyword_operator_regex.sub(code, " $1 ", true) # tab "a\t=" => "a =" code = RegEx.create_from_string(r"(\t*. * ?)\t * ").sub(code, "$1", true) #trim code = RegEx.create_from_string("[ \t]*\n").sub(code, "\n", true) # " " => " " code = RegEx.create_from_string(" +").sub(code, " ", true) # "= -a" => "= -a" code = RegEx.create_from_string(r"([=,(] ?)- ").sub(code, "$1-", true) return code static func _replace(text: String, what: String, forwhat: String) -> String: var index := text.find(what) if index != -1: text = text.substr(0, index) + forwhat + text.substr(index + what.length()) return text