Skip to content

Commit 6a786d7

Browse files
authored
Basic single file/dir with no mixfile support (#562)
* 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
1 parent 9a0843e commit 6a786d7

File tree

5 files changed

+327
-153
lines changed

5 files changed

+327
-153
lines changed

apps/language_server/lib/language_server/build.ex

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule ElixirLS.LanguageServer.Build do
22
alias ElixirLS.LanguageServer.{Server, JsonRpc, SourceFile, Diagnostics}
33

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

3838
{:error, mixfile_diagnostics} ->
3939
Server.build_finished(parent, {:error, mixfile_diagnostics})
40+
41+
:no_mixfile ->
42+
Server.build_finished(parent, {:no_mixfile, []})
4043
end
4144
end)
4245

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

168-
JsonRpc.log_message(:error, msg <> ". Looked for mixfile at #{inspect(mixfile)}")
169-
JsonRpc.show_message(:error, msg)
171+
JsonRpc.log_message(:info, msg <> ". Looked for mixfile at #{inspect(mixfile)}")
170172

171-
{:error, [mixfile_diagnostic({Path.absname(mixfile), nil, msg}, :error)]}
173+
:no_mixfile
172174
end
173175
end
174176

apps/language_server/lib/language_server/providers/formatting.ex

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,12 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
22
import ElixirLS.LanguageServer.Protocol, only: [range: 4]
33
alias ElixirLS.LanguageServer.SourceFile
44

5-
def format(%SourceFile{} = source_file, uri, project_dir) do
5+
def format(%SourceFile{} = source_file, uri, project_dir) when is_binary(project_dir) do
66
if can_format?(uri, project_dir) do
77
case SourceFile.formatter_opts(uri) do
88
{:ok, opts} ->
99
if should_format?(uri, project_dir, opts[:inputs]) do
10-
formatted = IO.iodata_to_binary([Code.format_string!(source_file.text, opts), ?\n])
11-
12-
response =
13-
source_file.text
14-
|> String.myers_difference(formatted)
15-
|> myers_diff_to_text_edits()
16-
17-
{:ok, response}
10+
do_format(source_file, opts)
1811
else
1912
{:ok, []}
2013
end
@@ -29,6 +22,21 @@ defmodule ElixirLS.LanguageServer.Providers.Formatting do
2922

3023
{:error, :internal_error, msg}
3124
end
25+
end
26+
27+
def format(%SourceFile{} = source_file, _uri, nil) do
28+
do_format(source_file)
29+
end
30+
31+
defp do_format(%SourceFile{text: text}, opts \\ []) do
32+
formatted = IO.iodata_to_binary([Code.format_string!(text, opts), ?\n])
33+
34+
response =
35+
text
36+
|> String.myers_difference(formatted)
37+
|> myers_diff_to_text_edits()
38+
39+
{:ok, response}
3240
rescue
3341
_e in [TokenMissingError, SyntaxError] ->
3442
{:error, :internal_error, "Unable to format due to syntax error"}

0 commit comments

Comments
 (0)