diff --git a/Project.toml b/Project.toml index 2ad2d9f..0ae43fb 100644 --- a/Project.toml +++ b/Project.toml @@ -10,6 +10,7 @@ AutoHashEquals = "15f4f7f2-30c1-5605-9d31-71845cf9641f" Salsa = "1fbf2c77-44e2-4d5d-8131-0fa618a5c278" UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" TestItemDetection = "76b0de8b-5c4b-48ef-a724-914b33ca988d" +JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899" [extras] TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a" @@ -21,6 +22,7 @@ julia = "1.6" AutoHashEquals = "1,2" Salsa = "2.2.0" TestItemDetection = "1.1" +JuliaFormatter = "1" [targets] test = ["Test", "TestItemRunner"] diff --git a/src/JuliaWorkspaces.jl b/src/JuliaWorkspaces.jl index b3fce9e..058b768 100644 --- a/src/JuliaWorkspaces.jl +++ b/src/JuliaWorkspaces.jl @@ -1,6 +1,6 @@ module JuliaWorkspaces -import UUIDs, JuliaSyntax, TestItemDetection +import UUIDs, JuliaSyntax, TestItemDetection, JuliaFormatter using UUIDs: UUID, uuid4 using JuliaSyntax: SyntaxNode using Salsa @@ -25,6 +25,7 @@ include("layer_files.jl") include("layer_syntax_trees.jl") include("layer_projects.jl") include("layer_testitems.jl") +include("layer_formatting.jl") include("layer_diagnostics.jl") include("fileio.jl") include("public.jl") diff --git a/src/fileio.jl b/src/fileio.jl index 2b9e950..ffdd167 100644 --- a/src/fileio.jl +++ b/src/fileio.jl @@ -20,6 +20,12 @@ function is_path_lintconfig_file(path) return basename_lower_case == ".julialint.toml" end +function is_path_formatterconfig_file(path) + basename_lower_case = basename(lowercase(path)) + + return basename_lower_case == ".juliaformatter.toml" +end + function is_path_julia_file(path) _, ext = splitext(lowercase(path)) @@ -54,6 +60,8 @@ function read_text_file_from_uri(uri::URI; return_nothing_on_io_error=false) "toml" elseif is_path_lintconfig_file(path) "toml" + elseif is_path_formatterconfig_file(path) + "toml" elseif is_path_markdown_file(path) "markdown" elseif is_path_juliamarkdown_file(path) @@ -95,6 +103,7 @@ function read_path_into_textdocuments(uri::URI; ignore_io_errors=false) is_path_project_file(filepath) || is_path_manifest_file(filepath) || is_path_lintconfig_file(filepath) || + is_path_formatterconfig_file(filepath) || is_path_markdown_file(filepath) || is_path_juliamarkdown_file(filepath) diff --git a/src/layer_formatting.jl b/src/layer_formatting.jl new file mode 100644 index 0000000..13aa1a0 --- /dev/null +++ b/src/layer_formatting.jl @@ -0,0 +1,66 @@ +Salsa.@derived function derived_formatterconfig_files(rt) + files = derived_text_files(rt) + + return [file for file in files if file.scheme=="file" && is_path_formatterconfig_file(uri2filepath(file))] +end + +struct RunicStyle <: JuliaFormatter.AbstractStyle +end + +Salsa.@derived function derived_formatter_configuration(rt, uri) + config_files = derived_formatterconfig_files(rt) + + config_files = sort(config_files, by=i->length(string(i)), rev=true) + + config_data = nothing + + for config_file in config_files + config_folder_path = dirname(uri2filepath(config_file)) + + if startswith(uri2filepath(uri), config_folder_path) + config_data = derived_toml_syntax_tree(rt, config_file) + break + end + end + + if config_data === nothing + config_data = Dict() + end + + if !haskey(config_data, "style") + config_data["style"] = "minimal" + end + + for (field, type) in fieldnts(JuliaFormatter.Options) + if type == Union{Bool,Nothing} + field = string(field) + if get(config_data, field, "") == "nothing" + config_data[field] = nothing + end + end + end + + valid_styles = ("minimal", "default", "yas", "blue", "sciml", "runic") + + if !(config_data["style"] in valid_styles) + return nothing + end + + config_data["style"] = if config_data["style"] == "minimal" + JuliaFormatter.MinimalStyle() + elseif config_data["style"] == "default" + JuliaFormatter.DefaultStyle() + elseif config_data["style"] == "yas" + JuliaFormatter.YASStyle() + elseif config_data["style"] == "blue" + JuliaFormatter.BlueStyle() + elseif config_data["style"] == "sciml" + JuliaFormatter.SciMLStyle() + elseif config_data["style"] == "runic" + RunicStyle() + else + error("Invalid style") + end + + return config_data +end diff --git a/src/public.jl b/src/public.jl index 717717b..93c2d0d 100644 --- a/src/public.jl +++ b/src/public.jl @@ -171,3 +171,13 @@ function get_files_with_updated_testitems(jw::JuliaWorkspace) # println(stderr, graph) return derived_testitems_updated_since_mark(jw.runtime) end + +function get_formatted_content(jw::JuliaWorkspace, uri::URI) + config = derived_formatter_configuration(jw.runtime, uri) + + if config===nothing + return nothing + end + + +end