~kiwec/ke

ke/src/Line.h -rw-r--r-- 2.2 KiB
1cc8481d — Wolf Clément Fix compilation 3 years ago
                                                                                
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
#pragma once
#include <stdint.h>
#include <memory>
#include "Syntax.h"

// TODO remove unique_ptr

class Buffer;
class Text;
using uint = uint32_t;

/// Line objects are temporary and should NOT be stored
/// Any modification to the linked Data would break the class
class Line {
protected:
	Text* m_doc;

	/// Line start in document
	uint m_start;

	/// Line length
	uint m_size = 0;

	/// Line number
	uint m_number = 1;

	/// Initializes m_size
	void calc_size();

public:
	/// Line from document
	Line(Text* doc, uint start_char, uint line_number) : m_doc(doc), m_start(start_char), m_number(line_number) {
		calc_size();
	}

	wchar_t operator[](uint pos) const;
	wchar_t* begin() const;
	wchar_t* end() const;

	/// Clears the Line's syntax
	void clear_syntax();

	/// Erases the given range in the line
	void erase(uint start, uint size);
	/// Erases the next line
	void erase_next();
	/// Erases the previous line
	void erase_prev();

	/// Get the next Line
	/// Check if is_last() first !
	Line next() const;

	/// Get previous Line
	/// Check if is_first() first !
	Line prev() const;

	/// Create a new Line
	Line make_next();

	/// Returns the character at which the line starts
	uint start() const { return m_start; }

	/// Return Line number (starts at 1)
	uint number() const { return m_number; }

	/// Return Line length
	uint length() const { return m_size; }

	int get_current_indentation() const;
	int get_next_indentation() const;

	bool is_first() const;
	bool is_last() const;
	void insert(wchar_t c, uint pos);
	void insert(wchar_t* str, uint size, uint pos);

	/// Print on screen with syntax highlighting
	void print(uint x, uint y, uint bg = 0) const;

	/// Print on screen with syntax highlighting, from start and up
	void printFrom(uint start, uint x, uint y, uint bg = 0) const;

	/// Returns the number of lines the line would take in a specific buffer
	uint height(Buffer& buf) const;

	/// Returns the matching syntax data
	syntax_t* syntax() const;

	/// Get a copy of the contained text
	std::unique_ptr<wchar_t[]> text_copy() const;

	/// Returns max width of the line, counting tabs as 4 characters
	uint width() const;

	/// Returns the width of the line, up to the given position
	uint width(uint pos) const;
};