summaryrefslogtreecommitdiff
path: root/src/lib/elementary/elm_code_line.c
blob: 2b3281de6681a1e2883da5a594594086ac796d21 (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
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
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include "Elementary.h"

#include "elm_code_private.h"

EAPI void
elm_code_line_free(Elm_Code_Line *line)
{
   if (!line)
     return;

   if (line->status_text)
     free((char *)line->status_text);
   if (line->modified)
     free(line->modified);

   free(line);
}

static void
_elm_code_line_tokens_split_at(Elm_Code_Line *oldline, Elm_Code_Line *newline,
                               Eina_List *tokens, int position)
{
   Eina_List *item, *next;
   Elm_Code_Token *token, *newtoken;

   EINA_LIST_FOREACH_SAFE(tokens, item, next, token)
     {
        if (!token->continues && token->end < position)
          {
             oldline->tokens = eina_list_append(oldline->tokens, token);
             continue;
          }
        if (token->start >= position)
          {
             token->start -= position;
             token->end -= position;
             newline->tokens = eina_list_append(newline->tokens, token);
             continue;
          }

        if (token->continues)
          elm_code_line_token_add(newline, 0, token->end, 1, token->type);
        else
          {
             elm_code_line_token_add(newline, 0, token->end - position, 1, token->type);
             token->end = position - 1;
          }

        newtoken = eina_list_data_get(newline->tokens);
        newtoken->continues = token->continues;
        token->continues = EINA_TRUE;
        oldline->tokens = eina_list_append(oldline->tokens, token);
     }

   elm_code_callback_fire(oldline->file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, oldline);
   elm_code_callback_fire(newline->file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, newline);
}

EAPI void elm_code_line_split_at(Elm_Code_Line *line, unsigned int position)
{
   Elm_Code_Line *newline;
   Elm_Code_Token *token EINA_UNUSED;
   Eina_List *tokens;
   char *content;
   unsigned int length;

   content = (char *) elm_code_line_text_get(line, &length);
   content = strndup(content, length);
   elm_code_file_line_insert(line->file, line->number + 1, "", 0, NULL);
   newline = elm_code_file_line_get(line->file, line->number + 1);

   tokens = line->tokens;
   line->tokens = NULL;
   elm_code_line_text_set(newline, content + position, length - position);
   elm_code_line_text_set(line, content, position);
   _elm_code_line_tokens_split_at(line, newline, tokens, position);

   EINA_LIST_FREE(tokens, token) {} // don't free tokens, we re-used them
   free(content);
}

static void
_elm_code_line_merge_into(Elm_Code_Line *line1, Elm_Code_Line *line2)
{
   Eina_List *tokens1, *tokens2;
   Elm_Code_Token *token;
   const char *text1, *text2;
   char *newtext;
   unsigned int length1, length2;

   text1 = elm_code_line_text_get(line1, &length1);
   text2 = elm_code_line_text_get(line2, &length2);

   newtext = malloc(sizeof(char) * (length1 + length2 + 1));
   snprintf(newtext, length1 + 1, "%s", text1);
   snprintf(newtext + length1, length2 + 1, "%s", text2);

   tokens1 = line1->tokens;
   line1->tokens = NULL;
   tokens2 = line2->tokens;
   line2->tokens = NULL;
   elm_code_file_line_remove(line2->file, line2->number);
   elm_code_line_text_set(line1, newtext, length1 + length2);

   EINA_LIST_FREE(tokens1, token)
     {
        token->continues = EINA_FALSE;
        line1->tokens = eina_list_append(line1->tokens, token);
     }
   EINA_LIST_FREE(tokens2, token)
     {
        token->start += length1;
        token->end += length1;

        line1->tokens = eina_list_append(line1->tokens, token);
     }

   elm_code_callback_fire(line1->file->parent, &ELM_CODE_EVENT_LINE_LOAD_DONE, line1);
   free(newtext);
}

EAPI void
elm_code_line_merge_up(Elm_Code_Line *line)
{
   Elm_Code_Line *other;

   other = elm_code_file_line_get(line->file, line->number - 1);

   if (other)
     _elm_code_line_merge_into(other, line);
}

EAPI void
elm_code_line_merge_down(Elm_Code_Line *line)
{
   Elm_Code_Line *other;

   other = elm_code_file_line_get(line->file, line->number + 1);

   if (other)
     _elm_code_line_merge_into(line, other);
}

EAPI void elm_code_line_status_set(Elm_Code_Line *line, Elm_Code_Status_Type status)
{
   if (!line)
     return;

   line->status = status;
}

EAPI void elm_code_line_status_text_set(Elm_Code_Line *line, const char *text)
{
   if (line->status_text)
     free(line->status_text);

   if (text)
     line->status_text = strdup(text);
   else
     line->status_text = NULL;
}

EAPI void elm_code_line_token_add(Elm_Code_Line *line, int start, int end, int lines,
                                  Elm_Code_Token_Type type)
{
   Elm_Code_Token *tok;
   Elm_Code_Line *next_line;

   if (!line)
     return;

   tok = calloc(1, sizeof(Elm_Code_Token));

   tok->start = start;
   tok->end = end;
   tok->continues = lines > 1;
   tok->type = type;

   line->tokens = eina_list_append(line->tokens, tok);

   if (lines > 1)
     {
        next_line = elm_code_file_line_get(line->file, line->number + 1);
        elm_code_line_token_add(next_line, 0, end, lines - 1, type);
     }
}

EAPI void elm_code_line_tokens_clear(Elm_Code_Line *line)
{
   Elm_Code_Token *token;

   if (!line->tokens)
     return;

   EINA_LIST_FREE(line->tokens, token)
     free(token);
   line->tokens = NULL;
}

EAPI void elm_code_line_status_clear(Elm_Code_Line *line)
{
   line->status = ELM_CODE_STATUS_TYPE_DEFAULT;
   if (line->status_text)
     {
        free((char *)line->status_text);
        line->status_text = NULL;
     }
}

EAPI Eina_Bool
elm_code_line_contains_widget_cursor(Elm_Code_Line *line)
{
   Elm_Code *code = line->file->parent;
   Eina_List *item;
   Eo *widget;
   unsigned int col, number;

   if (!code)
     return EINA_FALSE;

   EINA_LIST_FOREACH(code->widgets, item, widget)
     {
        elm_code_widget_cursor_position_get(widget, &col, &number);

        if (number == line->number)
          return EINA_TRUE;
     }

   return EINA_FALSE;
}