-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsavedFiles.cpp
More file actions
133 lines (111 loc) · 4.33 KB
/
UnsavedFiles.cpp
File metadata and controls
133 lines (111 loc) · 4.33 KB
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
#include "UnsavedFiles.hpp"
#include "CompileOptions.hpp"
#include "Styles.hpp"
#include "GlobalVariables.hpp"
UnsavedFiles UnsavedFiles::current;
static unsigned GetStyle(CXTranslationUnit unit, CXToken t, CXCursor c)
{
CXString spelling_ = clang_getTokenSpelling(unit, t);
wxString spelling = wxString::FromUTF8(clang_getCString(spelling_));
clang_disposeString(spelling_);
if (c.kind >= 500 && clang_getTokenKind(t) != CXToken_Comment)
return stPREPROCESSOR;
switch (clang_getTokenKind(t))
{
case CXToken_Punctuation:
return stPUNCTUATION;
case CXToken_Keyword:
if (typeKeywords.Index(spelling) != wxNOT_FOUND)
return stTYPE;
return stKEYWORD;
case CXToken_Identifier:
if (clang_isReference(c.kind) || c.kind == CXCursor_DeclRefExpr || c.kind == CXCursor_MemberRefExpr)
{
CXCursor c_ref = clang_getCursorReferenced(c);
return identifierStyle[c_ref.kind];
}
// fprintf(stderr, "%s, %d\n", clang_getCString(spelling_), c.kind);
return identifierStyle[c.kind];
case CXToken_Literal:
return stCONSTANT;
case CXToken_Comment:
return stCOMMENT;
}
return stINVALID;
}
void UnsavedFiles::PrepareSHData(cbEditor * editor, CXTranslationUnit unit, std::vector<char> & styles, std::vector<Diagnostic> & diagnostics) const
{
UnsavedFileData const& info = ufMap.find(editor)->second;
CXFile file = clang_getFile(unit, info.filename.get());
CXSourceLocation bl = clang_getLocationForOffset(unit, file, 0);
CXSourceLocation el = clang_getLocationForOffset(unit, file, (unsigned)info.length);
CXToken * tokens;
unsigned num_tokens;
clang_tokenize(unit, clang_getRange(bl, el), &tokens, &num_tokens);
CXCursor * cursors = new CXCursor[num_tokens];
clang_annotateTokens(unit, tokens, num_tokens, cursors);
styles.resize(info.length);
unsigned j = 0;
// for each token:
for (unsigned i = 0; i < num_tokens; ++i)
{
// fprintf(stderr, "%s\n", clang_getCString(clang_getTokenSpelling(unit, tokens[i])));
CXSourceRange range = clang_getTokenExtent(unit, tokens[i]);
unsigned a, b;
clang_getSpellingLocation(clang_getRangeStart(range), 0, 0, 0, &a);
clang_getSpellingLocation(clang_getRangeEnd(range), 0, 0, 0, &b);
for (; j < a; ++j)
styles[j] = stDEFAULT;
unsigned style = GetStyle(unit, tokens[i], cursors[i]);
for (; j < b; ++j)
styles[j] = style;
}
for (; j < info.length; ++j)
styles[j] = stDEFAULT;
diagnostics.clear();
for (unsigned i = 0, n = clang_getNumDiagnostics(unit); i < n; ++i)
{
CXDiagnostic diag = clang_getDiagnostic(unit, i);
CXDiagnosticSeverity kind = clang_getDiagnosticSeverity(diag);
if (kind != CXDiagnostic_Fatal && kind != CXDiagnostic_Error && kind != CXDiagnostic_Warning)
continue;
Diagnostic d;
d.kind = kind;
clang_getSpellingLocation(clang_getDiagnosticLocation(diag), 0, 0, 0, &d.offset);
CXString diag_cxstr = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
d.text = wxString::FromUTF8(clang_getCString(diag_cxstr));
diagnostics.push_back(d);
clang_disposeString(diag_cxstr);
clang_disposeDiagnostic(diag);
}
// cleanup
clang_disposeTokens(unit, tokens, num_tokens);
delete [] cursors;
}
std::vector<CXUnsavedFile> UnsavedFiles::GetUnsavedFiles() const
{
std::vector<CXUnsavedFile> data;
data.reserve(ufMap.size());
for (auto it = ufMap.begin(); it != ufMap.end(); ++it)
{
CXUnsavedFile uf;
uf.Filename = it->second.filename.get(); // safe
uf.Contents = it->second.contents.get(); // safe
uf.Length = it->second.length;
data.push_back(uf);
}
return data;
}
void UnsavedFiles::Print() const
{
fprintf(stderr, "==== BEGIN UNSAVED FILES ====\n");
for (auto it = ufMap.begin(); it != ufMap.end(); ++it)
{
fprintf(stderr, "EDITOR: %p\n", it->first);
fprintf(stderr, "\tFILENAME: %s\n", it->second.filename.get());
fprintf(stderr, "\tLENGTH: %lu\n", it->second.length);
fprintf(stderr, "\tVERSION: %u\n", it->second.version);
fprintf(stderr, "\tPROJECT: %p\n", it->second.project);
}
fprintf(stderr, "===== END UNSAVED FILES =====\n");
}