aboutsummaryrefslogtreecommitdiff
path: root/src/server/format.odin
blob: 57243ff614b46fe013e70f389ee6e41f4a844e4b (plain)
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
package server

import "shared:common"
import "shared:odin/printer"

import "core:log"

FormattingOptions :: struct {
	tabSize:                uint,
	insertSpaces:           bool, //tabs or spaces
	trimTrailingWhitespace: bool,
	insertFinalNewline:     bool,
	trimFinalNewlines:      bool,
}

DocumentFormattingParams :: struct {
	textDocument: TextDocumentIdentifier,
	options:      FormattingOptions,
}

get_complete_format :: proc(document: ^Document, config: ^common.Config) -> ([]TextEdit, bool) {
	style := printer.default_style
	style.tabs = config.formatter.tabs

	if config.formatter.characters != 0 {
		style.max_characters = config.formatter.characters
	}

	if config.formatter.spaces != 0 {
		style.spaces = config.formatter.spaces
	} 
	
	prnt := printer.make_printer(style, context.temp_allocator)

	if document.ast.syntax_error_count > 0 {
		return {}, true
	}

	if len(document.text) == 0 {
		return {}, true
	}

	src := printer.print(&prnt, &document.ast)

	log.error(src)

	end_line := 0
	end_charcter := 0

	last := document.text[0]
	line := 0

	for current_index := 0; current_index < len(document.text); current_index += 1 {
		current := document.text[current_index]

		if last == '\r' && current == '\n' {
			line += 1
			current_index += 1
		} else if current == '\n' {
			line += 1
		}

		last = current
	}

	edit := TextEdit {
		newText = src,
		range = {
			start = {
				character = 0,
				line = 0,
			},
			end = {
				character = 1,
				line = line+1,
			},
		},
	}

	edits := make([dynamic]TextEdit, context.temp_allocator)

	append(&edits, edit)

	return edits[:], true
}