mirror of
https://gitlab.gnome.org/jwestman/blueprint-compiler.git
synced 2025-05-04 15:59:08 -04:00
Merge branch 'blueprint-linter' into 'main'
Draft: GTK linter Closes #124 See merge request jwestman/blueprint-compiler!176
This commit is contained in:
commit
808d664301
3 changed files with 236 additions and 2 deletions
|
@ -24,8 +24,8 @@ import os
|
|||
import sys
|
||||
import typing as T
|
||||
|
||||
from . import formatter, interactive_port, parser, tokenizer
|
||||
from .errors import CompileError, CompilerBugError, PrintableError, report_bug
|
||||
from . import formatter, interactive_port, parser, tokenizer, linter
|
||||
from .errors import CompileError, CompileWarning, CompilerBugError, PrintableError, report_bug
|
||||
from .gir import add_typelib_search_path
|
||||
from .lsp import LanguageServer
|
||||
from .outputs import XmlOutput
|
||||
|
@ -103,6 +103,15 @@ class BlueprintApp:
|
|||
metavar="filenames",
|
||||
)
|
||||
|
||||
lint = self.add_subcommand(
|
||||
"lint", "Lint given blueprint files", self.cmd_lint
|
||||
)
|
||||
lint.add_argument(
|
||||
"inputs",
|
||||
nargs="+",
|
||||
metavar="filenames",
|
||||
)
|
||||
|
||||
port = self.add_subcommand("port", "Interactive porting tool", self.cmd_port)
|
||||
|
||||
lsp = self.add_subcommand(
|
||||
|
@ -300,6 +309,46 @@ class BlueprintApp:
|
|||
if panic:
|
||||
sys.exit(1)
|
||||
|
||||
def cmd_lint(self, opts):
|
||||
input_files = []
|
||||
missing_files = []
|
||||
panic = False
|
||||
|
||||
for path in opts.inputs:
|
||||
if os.path.isfile(path):
|
||||
input_files.append(path)
|
||||
elif os.path.isdir(path):
|
||||
for root, subfolders, files in os.walk(path):
|
||||
for file in files:
|
||||
if file.endswith(".blp"):
|
||||
input_files.append(os.path.join(root, file))
|
||||
else:
|
||||
missing_files.append(path)
|
||||
|
||||
for file in input_files:
|
||||
with open(file, "r+") as file:
|
||||
data = file.read()
|
||||
errored = False
|
||||
|
||||
tokens = tokenizer.tokenize(data)
|
||||
ast, errors, warnings = parser.parse(tokens)
|
||||
|
||||
if errors:
|
||||
raise errors
|
||||
if ast is None:
|
||||
raise CompilerBugError()
|
||||
|
||||
problems = linter.lint(ast)
|
||||
for problem in problems:
|
||||
if isinstance(problem, CompileError):
|
||||
problem.pretty_print(file.name, problem.range.original_text, stream=sys.stderr)
|
||||
panic = True
|
||||
elif isinstance(problem, CompileWarning):
|
||||
problem.pretty_print(file.name, problem.range.original_text, stream=sys.stderr)
|
||||
|
||||
if panic:
|
||||
sys.exit(1)
|
||||
|
||||
def cmd_lsp(self, opts):
|
||||
langserv = LanguageServer()
|
||||
langserv.run()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue