-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy path.iex.exs
156 lines (127 loc) · 3.34 KB
/
.iex.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
alias Lexical.RemoteControl
alias Lexical.Document
alias Lexical.Document.Position
defmodule Helpers do
alias Lexical.Document.Position
alias Lexical.Project
alias Lexical.Protocol.Types.Completion
alias Lexical.Server.CodeIntelligence
def observer do
:observer.start()
end
def observer(project) do
project
|> ensure_project()
|> RemoteControl.call(:observer, :start)
end
def doc(text) do
doc(:lexical, text)
end
def doc(project, text) do
root_path =
project
|> project()
|> Project.root_path()
[root_path, "lib", "file.ex"]
|> Path.join()
|> Document.Path.to_uri()
|> Document.new(text, 0)
end
def pos(line, character) do
Position.new(line, character)
end
def compile_project(project) do
project
|> ensure_project()
|> RemoteControl.Api.schedule_compile(true)
end
def compile_file(project, source) when is_binary(source) do
project
|> ensure_project()
|> compile_file(doc(source))
end
def compile_file(project, %Document{} = document) do
project
|> ensure_project()
|> RemoteControl.Api.compile_document(document)
end
def complete(project, source, context \\ nil)
def complete(project, source, context) when is_binary(source) do
case completion_position(source) do
{:found, line, character} ->
complete(project, doc(source), line, character, context)
other ->
other
end
end
def complete(project, %Document{} = source, line, character, context) do
context =
if is_nil(context) do
Completion.Context.new(trigger_kind: nil)
else
context
end
position = pos(line, character)
project
|> ensure_project()
|> CodeIntelligence.Completion.complete(source, position, context)
end
def connect do
manager_name = manager_name()
Node.start(:"[email protected]")
Node.set_cookie(:lexical)
Node.connect(:"#{manager_name}@127.0.0.1")
end
def project(project) when is_atom(project) do
project_path =
[File.cwd!(), "..", to_string(project)]
|> Path.join()
|> Path.expand()
project_uri = "file://#{project_path}"
Lexical.Project.new(project_uri)
end
def stop_project(project) do
project
|> ensure_project()
|> Lexical.Server.Project.Supervisor.stop()
end
def start_project(project) do
project
|> ensure_project()
|> Lexical.Server.Project.Supervisor.start()
end
defp manager_name do
{:ok, names} = :erl_epmd.names()
names
|> Enum.map(fn {name, _port} -> List.to_string(name) end)
|> Enum.find(&String.starts_with?(&1, "manager"))
end
defp completion_position(source_string) do
source_string
|> String.split(["\r\n", "\r", "\n"])
|> Enum.with_index()
|> Enum.reduce_while(:not_found, fn {line, line_number}, _ ->
if String.contains?(line, "|") do
index =
line
|> String.graphemes()
|> Enum.find_index(&(&1 == "|"))
{:halt, {:found, line_number + 1, index + 1}}
else
{:cont, :not_found}
end
end)
end
defp ensure_project(%Project{} = project) do
project
end
defp ensure_project(project) when is_binary(project) do
project
|> String.to_atom()
|> project()
end
defp ensure_project(project) when is_atom(project) do
project(project)
end
end
import Helpers