-
Notifications
You must be signed in to change notification settings - Fork 1
/
hug.gleam
459 lines (404 loc) · 10.3 KB
/
hug.gleam
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
////
//// - **Outputs**
//// - [`error`](#error)
//// - [`warning`](#error)
//// - [`info`](#info)
////
//// ---
////
//// <details>
//// <summary>The license of that package is produced below:</summary>
////
////
//// > MIT License
////
//// > Permission is hereby granted, free of charge, to any person obtaining a copy
//// of this software and associated documentation files (the "Software"), to deal
//// in the Software without restriction, including without limitation the rights
//// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//// copies of the Software, and to permit persons to whom the Software is
//// furnished to do so, subject to the following conditions:
////
//// > The above copyright notice and this permission notice shall be included in all
//// copies or substantial portions of the Software.
//// </details>
////
// IMPORTS --------------------------------------------------------------------
import gleam/int
import gleam/list
import gleam/result
import gleam/string
import gleam/option as o
import gleam_community/ansi
// TYPES ----------------------------------------------------------------------
type Location {
Location(row: Int, col: Int)
}
type Output {
Error
Warning
Info
}
pub opaque type HugBuilder {
HugBuilder(
file_name: o.Option(String),
source: o.Option(String),
start: o.Option(Location),
end: o.Option(Location),
message: o.Option(String),
hint: o.Option(String),
)
}
// pub type Options {
// Options
// }
// CONSTRUCTOR ----------------------------------------------------------------
// pub fn new() -> HugBuilder {
// HugBuilder(
// file_name: o.None,
// source: o.None,
// start: o.None,
// end: o.None,
// message: o.None,
// hint: o.None,
// )
// }
// pub fn with_file_name(builder: HugBuilder, file_name: String) -> HugBuilder {
// }
// OUTPUTS --------------------------------------------------------------------
/// Returns a `String` displaying the provided error and relevant code.
///
///
/// <details>
/// <summary>Example:</summary>
///
/// ```gleam
/// fn example() {
/// let source = "let five = 4 + 1.0"
///
/// source
/// |> hug.error(
/// in: "example.gleam",
/// from: #(1, 11),
/// to: #(1, 18),
/// message: "invalid type",
/// hint: "can not add an `Int` to a `Float`"
/// )
/// |> io.println()
/// }
/// ```
/// </details>
///
/// <div style="position: relative;">
/// <a style="position: absolute; left: 0;" href="https://github.com/brettkolodny/hug/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// <a style="position: absolute; right: 0;" href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn error(
in file_name: String,
containing source: String,
from start: #(Int, Int),
to end: #(Int, Int),
message msg: String,
hint hint: String,
) -> String {
output(
file_name,
source,
Location(row: start.0, col: start.1),
Location(row: end.0, col: end.1),
msg,
hint,
Error,
)
}
/// Returns a `String` displaying the provided warning and relevant code.
///
///
/// <details>
/// <summary>Example:</summary>
///
/// ```gleam
/// fn example() {
/// let source = "let five = 4 + 1.0"
///
/// source
/// |> hug.warning(
/// in: "example.gleam",
/// from: #(1, 5),
/// to: #(1, 9),
/// message: "unused variable",
/// hint: "the variable `five` is declared but never used"
/// )
/// |> io.println()
/// }
/// ```
/// </details>
///
/// <div style="position: relative;">
/// <a style="position: absolute; left: 0;" href="https://github.com/brettkolodny/hug/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// <a style="position: absolute; right: 0;" href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn warning(
in file_name: String,
containing source: String,
from start: #(Int, Int),
to end: #(Int, Int),
message msg: String,
hint hint: String,
) -> String {
output(
file_name,
source,
Location(row: start.0, col: start.1),
Location(row: end.0, col: end.1),
msg,
hint,
Warning,
)
}
/// Returns a `String` displaying the provided info and relevant code.
///
///
/// <details>
/// <summary>Example:</summary>
///
/// ```gleam
/// fn example() {
/// let source = "try five = int.parse("4") |> result.then(fn(v) { v + 1})"
///
/// source
/// |> hug.info(
/// in: "example.gleam",
/// from: #(1, 1),
/// to: #(1, 4),
/// message: "use of deprecated code",
/// hint: "`try` is marked as deprecated, check out `use`!"
/// )
/// |> io.println()
/// }
/// ```
/// </details>
///
/// <div style="position: relative;">
/// <a style="position: absolute; left: 0;" href="https://github.com/brettkolodny/hug/issues">
/// <small>Spot a typo? Open an issue!</small>
/// </a>
/// <a style="position: absolute; right: 0;" href="#">
/// <small>Back to top ↑</small>
/// </a>
/// </div>
///
pub fn info(
in file_name: String,
containing source: String,
from start: #(Int, Int),
to end: #(Int, Int),
message msg: String,
hint hint: String,
) -> String {
output(
file_name,
source,
Location(row: start.0, col: start.1),
Location(row: end.0, col: end.1),
msg,
hint,
Info,
)
}
// ----------------------------------------------------------------------------
fn output(
file_name: String,
source: String,
start: Location,
end: Location,
err: String,
hint: String,
output: Output,
) {
let header = construct_header(err, output)
let body = construct_body(file_name, source, start, end, output)
string.join([header, body, "", hint], "\n")
}
//
fn get_relevant_lines(
source_lines: List(String),
start: Location,
end: Location,
) -> List(String) {
use lines, line, index <- list.index_fold(source_lines, [])
case index + 1 >= start.row && index + 1 <= end.row {
True -> list.append(lines, [line])
False -> lines
}
}
//
fn underline_source(
source_lines: List(String),
start: Location,
end: Location,
output: Output,
) -> List(String) {
let colour = case output {
Error -> ansi.red
Warning -> ansi.yellow
Info -> ansi.blue
}
use line, index <- list.index_map(source_lines)
let list_length = list.length(source_lines) - 1
case string.trim(line) {
"" -> ""
_ ->
case index {
i if i == 0 -> {
let white_space = string.repeat(" ", start.col - 1)
let underline_end = case end.row == start.row {
True -> end.col - start.col
False -> string.length(line) - string.length(white_space)
}
white_space <> colour(string.repeat("~", underline_end))
}
i if i == list_length -> {
let line_length = string.length(line)
let line_length_post_trim = string.length(string.trim_left(line))
let num_white_space = line_length - line_length_post_trim
let white_space = string.repeat(" ", num_white_space)
white_space
<> colour(string.repeat("~", end.col - num_white_space - 1))
}
_ -> {
let line_length = string.length(line)
let line_length_post_trim = string.length(string.trim_left(line))
let num_white_space = line_length - line_length_post_trim
let white_space = string.repeat(" ", num_white_space)
white_space <> colour(string.repeat("~", line_length_post_trim))
}
}
}
}
fn construct_header(message: String, output: Output) -> String {
case output {
Error -> ansi.red("error: ") <> message
Warning -> ansi.yellow("warning: ") <> message
Info -> ansi.blue("info: ") <> message
}
}
//
fn construct_body(
file_name: String,
source: String,
start: Location,
end: Location,
output: Output,
) -> String {
let left_padding =
int.max(
string.length(int.to_string(start.row)),
string.length(int.to_string(end.row)),
)
- 1
let body_start =
string.repeat(" ", left_padding)
<> " ┌─ "
<> file_name
<> ":"
<> int.to_string(start.row)
<> ":"
<> int.to_string(start.col)
let relevant_lines =
get_relevant_lines(string.split(source, on: "\n"), start, end)
let underlines = underline_source(relevant_lines, start, end, output)
let trim_left_amount = get_trim_left_amount(relevant_lines)
let body =
list.zip(relevant_lines, underlines)
|> list.index_map(fn(input, index) {
construct_output_line(
input,
index
+ start.row,
trim_left_amount,
left_padding,
)
})
|> string.join("\n")
string.join(
[
body_start,
string.repeat(" ", left_padding)
<> " │",
body,
string.repeat(" ", left_padding)
<> " │",
],
"\n",
)
}
//
fn construct_output_line(
input: #(String, String),
row: Int,
trim_left_amount: Int,
left_padding: Int,
) -> String {
let #(source_line, underline) = input
let line_number_padding = left_padding - string.length(int.to_string(row)) + 1
let source_line =
ansi.green(int.to_string(row))
<> string.repeat(" ", line_number_padding)
<> " │ "
<> trim_left(source_line, by: trim_left_amount)
case string.length(underline) {
0 -> source_line
_ -> {
let underline_line =
string.repeat(" ", left_padding)
<> " │ "
<> trim_left(underline, by: trim_left_amount)
string.join([source_line, underline_line], "\n")
}
}
}
//
fn get_trim_left_amount(lines: List(String)) -> Int {
let get_left_white_space = fn(line) {
string.length(line)
- {
line
|> string.trim_left()
|> string.length()
}
}
let first_line =
list.first(lines)
|> result.unwrap("")
use min_white_space, line <- list.fold(lines, get_left_white_space(first_line),
)
case string.trim(line) {
"" -> min_white_space
_ -> {
let white_space =
string.length(line)
- {
line
|> string.trim_left()
|> string.length()
}
int.min(min_white_space, white_space)
}
}
}
//
fn trim_left(str: String, by num_white_space: Int) -> String {
let string_length = string.length(str)
string.slice(from: str, at_index: num_white_space, length: string_length)
}