From 63e4eef48bc375d14353206c2e9c1a7105e89113 Mon Sep 17 00:00:00 2001 From: Miraculous Ladybugreport <3642643+PeyTy@users.noreply.github.com> Date: Fri, 21 Jun 2024 16:47:21 +0300 Subject: [PATCH] [SERVER] Auto formatting draft --- hexa.json | 1 + source/server.hexa | 8 ++++++++ source/server/autoformat.hexa | 1 - source/server/format.hexa | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) delete mode 100644 source/server/autoformat.hexa create mode 100644 source/server/format.hexa diff --git a/hexa.json b/hexa.json index baf752b..a953631 100644 --- a/hexa.json +++ b/hexa.json @@ -33,6 +33,7 @@ "main", "cli/symlink", "cli/init", + "server/format", "server" ], "define": diff --git a/source/server.hexa b/source/server.hexa index f8bbcd3..5abb3ed 100644 --- a/source/server.hexa +++ b/source/server.hexa @@ -30,6 +30,7 @@ enum ServerCommandKind : String { FindProjectFile // Uses `FindProjectFile` internally + // TODO reanme to AutoCheck AutocheckProject // IntelliSense @@ -37,6 +38,9 @@ enum ServerCommandKind : String { DocumentSymbolProvider HoverProvider + // Refactoring + WholeDocumentFormatting + // Deprecated commands here } @@ -677,6 +681,10 @@ class Server { } } + case WholeDocumentFormatting: { + response.push({content: autoFormatWholeFile(command.payload)}) + } + case SyncFileContents: let fsPath: String = command.payload.fsPath let content: String = command.payload.content diff --git a/source/server/autoformat.hexa b/source/server/autoformat.hexa deleted file mode 100644 index 70b786d..0000000 --- a/source/server/autoformat.hexa +++ /dev/null @@ -1 +0,0 @@ -// TODO diff --git a/source/server/format.hexa b/source/server/format.hexa new file mode 100644 index 0000000..df5e1b9 --- /dev/null +++ b/source/server/format.hexa @@ -0,0 +1,32 @@ +// The Hexa Compiler +// Copyright (C) 2024 Oleh Petrenko +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, version 3 of the License. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . + +fun autoFormatWholeFile(file: String): String { + let project = new Project() + let content = Buffer.from(file) + try { + let tokens = Lexer.tokenize(content, 'hexa.hexa') + try { + let nodes = Parser.parseNodes(tokens, project, lint: true) + return file + } catch e: CompilerErrors { + return file + } + } catch e: CompilerErrors { + return file + } + + return file +}