Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start to add formatting #33

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,6 +22,7 @@ julia = "1.6"
AutoHashEquals = "1,2"
Salsa = "2.2.0"
TestItemDetection = "1.1"
JuliaFormatter = "1"

[targets]
test = ["Test", "TestItemRunner"]
3 changes: 2 additions & 1 deletion src/JuliaWorkspaces.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module JuliaWorkspaces

import UUIDs, JuliaSyntax, TestItemDetection
import UUIDs, JuliaSyntax, TestItemDetection, JuliaFormatter
using UUIDs: UUID, uuid4
using JuliaSyntax: SyntaxNode
using Salsa
Expand All @@ -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")
Expand Down
9 changes: 9 additions & 0 deletions src/fileio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
66 changes: 66 additions & 0 deletions src/layer_formatting.jl
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions src/public.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading