Skip to content

Commit

Permalink
Basic single file/dir with no mixfile support (#562)
Browse files Browse the repository at this point in the history
* do not error when no mixfile

* add tests

* do not warn when no mixfile

* assert project dir is set when building

* pattern match module type

* use path api

* do nat try to return test lenses when project_dir is nil

* add assertion

* format with default options when project_dir is nil

* undo wrong assertion

* fix logic error - do not set needs_build? when build not enabled

* show no mixfile message only once instead of on every trigger_build

* run formatter

* add tests
  • Loading branch information
lukaszsamson authored Jun 30, 2021
1 parent 9a0843e commit 6a786d7
Show file tree
Hide file tree
Showing 5 changed files with 327 additions and 153 deletions.
10 changes: 6 additions & 4 deletions apps/language_server/lib/language_server/build.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule ElixirLS.LanguageServer.Build do
alias ElixirLS.LanguageServer.{Server, JsonRpc, SourceFile, Diagnostics}

def build(parent, root_path, opts) do
def build(parent, root_path, opts) when is_binary(root_path) do
if Path.absname(File.cwd!()) != Path.absname(root_path) do
IO.puts("Skipping build because cwd changed from #{root_path} to #{File.cwd!()}")
{nil, nil}
Expand Down Expand Up @@ -37,6 +37,9 @@ defmodule ElixirLS.LanguageServer.Build do

{:error, mixfile_diagnostics} ->
Server.build_finished(parent, {:error, mixfile_diagnostics})

:no_mixfile ->
Server.build_finished(parent, {:no_mixfile, []})
end
end)

Expand Down Expand Up @@ -165,10 +168,9 @@ defmodule ElixirLS.LanguageServer.Build do
"No mixfile found in project. " <>
"To use a subdirectory, set `elixirLS.projectDir` in your settings"

JsonRpc.log_message(:error, msg <> ". Looked for mixfile at #{inspect(mixfile)}")
JsonRpc.show_message(:error, msg)
JsonRpc.log_message(:info, msg <> ". Looked for mixfile at #{inspect(mixfile)}")

{:error, [mixfile_diagnostic({Path.absname(mixfile), nil, msg}, :error)]}
:no_mixfile
end
end

Expand Down
26 changes: 17 additions & 9 deletions apps/language_server/lib/language_server/providers/formatting.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
import ElixirLS.LanguageServer.Protocol, only: [range: 4]
alias ElixirLS.LanguageServer.SourceFile

def format(%SourceFile{} = source_file, uri, project_dir) do
def format(%SourceFile{} = source_file, uri, project_dir) when is_binary(project_dir) do
if can_format?(uri, project_dir) do
case SourceFile.formatter_opts(uri) do
{:ok, opts} ->
if should_format?(uri, project_dir, opts[:inputs]) do
formatted = IO.iodata_to_binary([Code.format_string!(source_file.text, opts), ?\n])

response =
source_file.text
|> String.myers_difference(formatted)
|> myers_diff_to_text_edits()

{:ok, response}
do_format(source_file, opts)
else
{:ok, []}
end
Expand All @@ -29,6 +22,21 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do

{:error, :internal_error, msg}
end
end

def format(%SourceFile{} = source_file, _uri, nil) do
do_format(source_file)
end

defp do_format(%SourceFile{text: text}, opts \\ []) do
formatted = IO.iodata_to_binary([Code.format_string!(text, opts), ?\n])

response =
text
|> String.myers_difference(formatted)
|> myers_diff_to_text_edits()

{:ok, response}
rescue
_e in [TokenMissingError, SyntaxError] ->
{:error, :internal_error, "Unable to format due to syntax error"}
Expand Down
Loading

0 comments on commit 6a786d7

Please sign in to comment.