Skip to content

Commit 2762352

Browse files
authored
Refactor: Lexical Core naming (lexical-lsp#129)
* Refactor: Lexical Core naming The name `SourceFile` was carried over from elixir-ls, and I have never been enamored with the name, since it's long and kind of a lie. They're not really files. Seems to me, they're Documents, which is what the language server spec calls them. To that end, here are the name changes I made: Lexical.DocumentContainer => Lexical.Document.Container Lexical.SourceFile => Lexical.Document Lexical.SourceFile.Document => Lexical.Document.Lines Lexical.SourceFile.DocumentEdits => Lexical.Document.Changes Lexical.SourceFile.Edit => Lexical.Document.Edit Lexical.SourceFile.Position => Lexical.Document.Position Lexical.SourceFile.Range => Lexical.Document.Range I feel that this naming is a lot nicer than before
1 parent 8bc245b commit 2762352

File tree

90 files changed

+889
-892
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+889
-892
lines changed

apps/common/lib/lexical/convertible.ex

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
defmodule Lexical.Convertible.Helpers do
2+
alias Lexical.Document
3+
24
def apply(%{} = map, func, context_document) do
35
Enum.reduce_while(map, [], fn {key, value}, acc ->
4-
context_document = Lexical.DocumentContainer.context_document(value, context_document)
6+
context_document = Document.Container.context_document(value, context_document)
57

68
case func.(value, context_document) do
79
{:ok, native} ->
@@ -16,7 +18,7 @@ defmodule Lexical.Convertible.Helpers do
1618
def apply(enumerable, func, context_document) do
1719
result =
1820
Enum.reduce_while(enumerable, [], fn elem, acc ->
19-
context_document = Lexical.DocumentContainer.context_document(elem, context_document)
21+
context_document = Document.Container.context_document(elem, context_document)
2022

2123
case func.(elem, context_document) do
2224
{:ok, native} ->
@@ -38,7 +40,7 @@ defmodule Lexical.Convertible.Helpers do
3840
end
3941

4042
defprotocol Lexical.Convertible do
41-
alias Lexical.DocumentContainer
43+
alias Lexical.Document
4244

4345
@fallback_to_any true
4446

@@ -52,13 +54,13 @@ defprotocol Lexical.Convertible do
5254
@doc """
5355
Converts the structure to a native implementation
5456
"""
55-
@spec to_native(t, DocumentContainer.maybe_context_document()) :: native_response()
57+
@spec to_native(t, Document.Container.maybe_context_document()) :: native_response()
5658
def to_native(t, context_document)
5759

5860
@doc """
5961
Converts the native representation to a LSP compatible struct
6062
"""
61-
@spec to_lsp(t, DocumentContainer.maybe_context_document()) :: lsp_response()
63+
@spec to_lsp(t, Document.Container.maybe_context_document()) :: lsp_response()
6264
def to_lsp(t, context_document)
6365
end
6466

@@ -108,11 +110,11 @@ end
108110

109111
defimpl Lexical.Convertible, for: Any do
110112
alias Lexical.Convertible
111-
alias Lexical.DocumentContainer
113+
alias Lexical.Document
112114
alias Lexical.Convertible.Helpers
113115

114116
def to_native(%_struct_module{} = struct, context_document) do
115-
context_document = DocumentContainer.context_document(struct, context_document)
117+
context_document = Document.Container.context_document(struct, context_document)
116118

117119
result =
118120
struct
@@ -133,7 +135,7 @@ defimpl Lexical.Convertible, for: Any do
133135
end
134136

135137
def to_lsp(%_struct_module{} = struct, context_document) do
136-
context_document = DocumentContainer.context_document(struct, context_document)
138+
context_document = Document.Container.context_document(struct, context_document)
137139

138140
result =
139141
struct
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
1-
defmodule Lexical.SourceFile do
1+
defmodule Lexical.Document do
22
@moduledoc """
33
A representation of a LSP text document
44
5-
A source file is the fundamental data structure of the Lexical language server.
5+
A document is the fundamental data structure of the Lexical language server.
66
All language server documents are represented and backed by source files, which
77
provide functionality for fetching lines, applying changes, and tracking versions.
88
"""
99
alias Lexical.Convertible
10-
alias Lexical.SourceFile.Document
11-
alias Lexical.SourceFile.Edit
12-
alias Lexical.SourceFile.Line
13-
alias Lexical.SourceFile.Position
14-
alias Lexical.SourceFile.Range
10+
alias Lexical.Document.Edit
11+
alias Lexical.Document.Line
12+
alias Lexical.Document.Lines
13+
alias Lexical.Document.Position
14+
alias Lexical.Document.Range
1515

16-
import Lexical.SourceFile.Line
16+
import Lexical.Document.Line
1717

18-
alias __MODULE__.Path, as: SourceFilePath
18+
alias __MODULE__.Path, as: DocumentPath
1919

20-
@derive {Inspect, only: [:path, :version, :dirty?, :document]}
20+
@derive {Inspect, only: [:path, :version, :dirty?, :lines]}
2121

22-
defstruct [:uri, :path, :version, dirty?: false, document: nil]
22+
defstruct [:uri, :path, :version, dirty?: false, lines: nil]
2323

2424
@type version :: non_neg_integer()
2525
@type t :: %__MODULE__{
2626
uri: String.t(),
2727
version: version(),
2828
dirty?: boolean,
29-
document: Document.t(),
29+
lines: Lines.t(),
3030
path: String.t()
3131
}
3232

@@ -35,44 +35,44 @@ defmodule Lexical.SourceFile do
3535
# public
3636

3737
@doc """
38-
Creates a new source fie from a uri or path, the source code
38+
Creates a new document from a uri or path, the source code
3939
as a binary and the vewrsion.
4040
"""
4141
@spec new(Lexical.path() | Lexical.uri(), String.t(), version()) :: t
4242
def new(maybe_uri, text, version) do
43-
uri = SourceFilePath.ensure_uri(maybe_uri)
43+
uri = DocumentPath.ensure_uri(maybe_uri)
4444

4545
%__MODULE__{
4646
uri: uri,
4747
version: version,
48-
document: Document.new(text),
49-
path: SourceFilePath.from_uri(uri)
48+
lines: Lines.new(text),
49+
path: DocumentPath.from_uri(uri)
5050
}
5151
end
5252

5353
@doc """
5454
Returns the number of lines in the document
5555
"""
5656
@spec size(t) :: non_neg_integer()
57-
def size(%__MODULE__{} = source) do
58-
Document.size(source.document)
57+
def size(%__MODULE__{} = document) do
58+
Lines.size(document.lines)
5959
end
6060

6161
@doc """
62-
Marks the source file as dirty
62+
Marks the document file as dirty
6363
"""
6464
@spec mark_dirty(t) :: t
65-
def mark_dirty(%__MODULE__{} = source) do
66-
%__MODULE__{source | dirty?: true}
65+
def mark_dirty(%__MODULE__{} = document) do
66+
%__MODULE__{document | dirty?: true}
6767
end
6868

6969
@doc """
70-
Marks the source file as clean
70+
Marks the document file as clean
7171
"""
7272

7373
@spec mark_clean(t) :: t
74-
def mark_clean(%__MODULE__{} = source) do
75-
%__MODULE__{source | dirty?: false}
74+
def mark_clean(%__MODULE__{} = document) do
75+
%__MODULE__{document | dirty?: false}
7676
end
7777

7878
@doc """
@@ -81,16 +81,16 @@ defmodule Lexical.SourceFile do
8181
Returns {:ok, text} if the line exists, and :error if it doesn't
8282
"""
8383
@spec fetch_text_at(t, version()) :: {:ok, String.t()} | :error
84-
def fetch_text_at(%__MODULE__{} = source, line_number) do
85-
case fetch_line_at(source, line_number) do
84+
def fetch_text_at(%__MODULE__{} = document, line_number) do
85+
case fetch_line_at(document, line_number) do
8686
{:ok, line(text: text)} -> {:ok, text}
8787
_ -> :error
8888
end
8989
end
9090

9191
@spec fetch_line_at(t, version()) :: {:ok, Line.t()} | :error
92-
def fetch_line_at(%__MODULE__{} = source, line_number) do
93-
case Document.fetch_line(source.document, line_number) do
92+
def fetch_line_at(%__MODULE__{} = document, line_number) do
93+
case Lines.fetch_line(document.lines, line_number) do
9494
{:ok, line} -> {:ok, line}
9595
_ -> :error
9696
end
@@ -103,97 +103,97 @@ defmodule Lexical.SourceFile do
103103
{:error, :invalid_version}
104104
end
105105

106-
def apply_content_changes(%__MODULE__{} = source, _, []) do
107-
{:ok, source}
106+
def apply_content_changes(%__MODULE__{} = document, _, []) do
107+
{:ok, document}
108108
end
109109

110-
def apply_content_changes(%__MODULE__{} = source, version, changes) when is_list(changes) do
110+
def apply_content_changes(%__MODULE__{} = document, version, changes) when is_list(changes) do
111111
result =
112-
Enum.reduce_while(changes, source, fn
113-
nil, source ->
114-
{:cont, source}
112+
Enum.reduce_while(changes, document, fn
113+
nil, document ->
114+
{:cont, document}
115115

116-
change, source ->
117-
case apply_change(source, change) do
118-
{:ok, new_source} ->
119-
{:cont, new_source}
116+
change, document ->
117+
case apply_change(document, change) do
118+
{:ok, new_document} ->
119+
{:cont, new_document}
120120

121121
error ->
122122
{:halt, error}
123123
end
124124
end)
125125

126126
case result do
127-
%__MODULE__{} = source ->
128-
source = mark_dirty(%__MODULE__{source | version: version})
127+
%__MODULE__{} = document ->
128+
document = mark_dirty(%__MODULE__{document | version: version})
129129

130-
{:ok, source}
130+
{:ok, document}
131131

132132
error ->
133133
error
134134
end
135135
end
136136

137-
def to_string(%__MODULE__{} = source) do
138-
source
137+
def to_string(%__MODULE__{} = document) do
138+
document
139139
|> to_iodata()
140140
|> IO.iodata_to_binary()
141141
end
142142

143143
# private
144144

145-
defp line_count(%__MODULE__{} = source) do
146-
Document.size(source.document)
145+
defp line_count(%__MODULE__{} = document) do
146+
Lines.size(document.lines)
147147
end
148148

149149
defp apply_change(
150-
%__MODULE__{} = source,
150+
%__MODULE__{} = document,
151151
%Range{start: %Position{} = start_pos, end: %Position{} = end_pos},
152152
new_text
153153
) do
154154
start_line = start_pos.line
155155

156156
new_lines_iodata =
157157
cond do
158-
start_line > line_count(source) ->
159-
append_to_end(source, new_text)
158+
start_line > line_count(document) ->
159+
append_to_end(document, new_text)
160160

161161
start_line < 1 ->
162-
prepend_to_beginning(source, new_text)
162+
prepend_to_beginning(document, new_text)
163163

164164
true ->
165-
apply_valid_edits(source, new_text, start_pos, end_pos)
165+
apply_valid_edits(document, new_text, start_pos, end_pos)
166166
end
167167

168168
new_document =
169169
new_lines_iodata
170170
|> IO.iodata_to_binary()
171-
|> Document.new()
171+
|> Lines.new()
172172

173-
{:ok, %__MODULE__{source | document: new_document}}
173+
{:ok, %__MODULE__{document | lines: new_document}}
174174
end
175175

176-
defp apply_change(%__MODULE__{} = source, %Edit{range: nil} = edit) do
177-
{:ok, %__MODULE__{source | document: Document.new(edit.text)}}
176+
defp apply_change(%__MODULE__{} = document, %Edit{range: nil} = edit) do
177+
{:ok, %__MODULE__{document | lines: Lines.new(edit.text)}}
178178
end
179179

180-
defp apply_change(%__MODULE__{} = source, %Edit{range: %Range{}} = edit) do
180+
defp apply_change(%__MODULE__{} = document, %Edit{range: %Range{}} = edit) do
181181
if valid_edit?(edit) do
182-
apply_change(source, edit.range, edit.text)
182+
apply_change(document, edit.range, edit.text)
183183
else
184184
{:error, {:invalid_range, edit.range}}
185185
end
186186
end
187187

188-
defp apply_change(%__MODULE__{} = source, %{range: range, text: text}) do
189-
with {:ok, native_range} <- Convertible.to_native(range, source) do
190-
apply_change(source, Edit.new(text, native_range))
188+
defp apply_change(%__MODULE__{} = document, %{range: range, text: text}) do
189+
with {:ok, native_range} <- Convertible.to_native(range, document) do
190+
apply_change(document, Edit.new(text, native_range))
191191
end
192192
end
193193

194-
defp apply_change(%__MODULE__{} = source, convertable_edit) do
195-
with {:ok, edit} <- Convertible.to_native(convertable_edit, source) do
196-
apply_change(source, edit)
194+
defp apply_change(%__MODULE__{} = document, convertable_edit) do
195+
with {:ok, edit} <- Convertible.to_native(convertable_edit, document) do
196+
apply_change(document, edit)
197197
end
198198
end
199199

@@ -204,16 +204,16 @@ defmodule Lexical.SourceFile do
204204
end_pos.character >= 0
205205
end
206206

207-
defp append_to_end(%__MODULE__{} = source, edit_text) do
208-
[to_iodata(source), edit_text]
207+
defp append_to_end(%__MODULE__{} = document, edit_text) do
208+
[to_iodata(document), edit_text]
209209
end
210210

211-
defp prepend_to_beginning(%__MODULE__{} = source, edit_text) do
212-
[edit_text, to_iodata(source)]
211+
defp prepend_to_beginning(%__MODULE__{} = document, edit_text) do
212+
[edit_text, to_iodata(document)]
213213
end
214214

215-
defp apply_valid_edits(%__MODULE__{} = source, edit_text, start_pos, end_pos) do
216-
Document.reduce(source.document, [], fn line() = line, acc ->
215+
defp apply_valid_edits(%__MODULE__{} = document, edit_text, start_pos, end_pos) do
216+
Lines.reduce(document.lines, [], fn line() = line, acc ->
217217
case edit_action(line, edit_text, start_pos, end_pos) do
218218
:drop ->
219219
acc
@@ -268,11 +268,7 @@ defmodule Lexical.SourceFile do
268268
binary_part(text, start_index, length)
269269
end
270270

271-
defp to_iodata(%__MODULE__{} = source) do
272-
Document.to_iodata(source.document)
271+
defp to_iodata(%__MODULE__{} = document) do
272+
Lines.to_iodata(document.lines)
273273
end
274-
275-
# defp increment_version(%__MODULE__{} = source) do
276-
# %__MODULE__{source | version: source.version + 1}
277-
# end
278274
end

apps/common/lib/lexical/document_container.ex apps/common/lib/lexical/document/container.ex

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
defprotocol Lexical.DocumentContainer do
2-
alias Lexical.SourceFile
1+
defprotocol Lexical.Document.Container do
2+
alias Lexical.Document
33
@fallback_to_any true
4-
@type maybe_context_document :: SourceFile.t() | nil
4+
@type maybe_context_document :: Document.t() | nil
55

66
@spec context_document(t, maybe_context_document()) :: maybe_context_document()
77
def context_document(t, parent_context_document)
88
end
99

10-
defimpl Lexical.DocumentContainer, for: Any do
11-
alias Lexical.SourceFile
10+
defimpl Lexical.Document.Container, for: Any do
11+
alias Lexical.Document
1212

13-
def context_document(%{source_file: %SourceFile{} = source_file}, _) do
14-
source_file
13+
def context_document(%{document: %Document{} = document}, _) do
14+
document
1515
end
1616

1717
def context_document(%{lsp: lsp_request}, parent_context_document) do
1818
context_document(lsp_request, parent_context_document)
1919
end
2020

2121
def context_document(%{text_document: %{uri: uri}}, parent_context_document) do
22-
case SourceFile.Store.fetch(uri) do
23-
{:ok, source_file} -> source_file
22+
case Document.Store.fetch(uri) do
23+
{:ok, document} -> document
2424
_ -> parent_context_document
2525
end
2626
end

0 commit comments

Comments
 (0)