Skip to content

Commit ba99c57

Browse files
committed
src: workspace[update + reset + check]
1 parent 6c3c293 commit ba99c57

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/workspace.cr

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,61 @@ module MoonScript
5252
def ast: Ast | Error
5353
map_error(artifacts, &.ast)
5454
end
55+
56+
def nodes_at_path(path: String)
57+
map_error(ast, &.nodes_at_path(path))
58+
end
59+
60+
def formatter_config
61+
MoonJson.parse?(@path, search: true).try(&.formatter) || Formatter::Config.new
62+
end
63+
64+
def format(node: Ast::Node | Nil) : String | Nil
65+
Formatter.new(formatter_config).format!(node)
66+
end
67+
68+
def format(path: String) : String | Error | Nil
69+
case item = ast(path)
70+
in Ast
71+
Formatter.new(formatter_config).format(item)
72+
in Error, Nil
73+
item
74+
end
75+
end
76+
77+
def reset
78+
@cache.clear
79+
@watcher.patterns = SourceFiles.everything(MoonJson.parse(@path, search: true), include_tests: @include_tests, dot_env: @dot_env)
80+
rescue error: Error
81+
set(error)
82+
end
83+
84+
def check
85+
Logger.log "Type checking" do
86+
if error = @cache.values.select(Error).first?
87+
error
88+
else
89+
TypeChecker.new(check_everything: @check.unreachable?, check_env: @check.environment, ast: unchecked_ast).tap(&check)
90+
end
91+
rescue error: Error
92+
error
93+
end
94+
end
95+
96+
def update(files: Array(String), reason: Symbol)
97+
actions = [] of Symbol
98+
99+
Logger.log "Parsing files" do
100+
files.each do |file|
101+
if File.extname(file) == ".moon"
102+
if File.exists?(file)
103+
contents = File.read(file)
104+
update(contents, file)
105+
106+
if @format
107+
case ast = ast(file)
108+
when Ast formatted = Formatter.new(formatter_config).format(ast) + "\n"
109+
110+
end
55111
end
56112
end

0 commit comments

Comments
 (0)