Skip to content

Commit 24c02e6

Browse files
committedMay 17, 2016
Use diff-match-patch submodule, apply font and theme, major cleanup
1 parent ee86118 commit 24c02e6

File tree

3 files changed

+44
-96
lines changed

3 files changed

+44
-96
lines changed
 

‎.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "edbee-lib"]
22
path = edbee-lib
33
url = git@github.com:tomv564/edbee-lib
4+
[submodule "diff-match-patch-cpp-stl"]
5+
path = diff-match-patch-cpp-stl
6+
url = git@github.com:tomv564/diff-match-patch-cpp-stl

‎app/example2.cpp

+40-96
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ using namespace std;
1919
#include "edbee/texteditorwidget.h"
2020
#include "edbee/views/textrenderer.h"
2121
#include "edbee/views/texttheme.h"
22+
#include "edbee/models/texteditorconfig.h"
2223
//#include "diff_match_patch.h"
2324

2425
typedef diff_match_patch<string> stringdiff;
2526

26-
string getFileContents(const char* path)
27+
string getFileContents(string path)
2728
{
2829
ifstream myfile(path);
2930
if (myfile.is_open())
@@ -34,7 +35,7 @@ string getFileContents(const char* path)
3435
// TODO: who deallocates buffer and myfile?
3536
return buffer.str();
3637
}
37-
else qDebug() << "Unable to open file" << path;
38+
else qDebug() << "Unable to open file" << path.c_str();
3839
return NULL;
3940
}
4041

@@ -86,29 +87,6 @@ QVector<QVector<stringdiff::Diff>> createDiffLookup(list<stringdiff::Diff> diffs
8687
}
8788

8889
}
89-
90-
//for (int lineOffset = 0; lineOffset < lineCount; ++lineOffset) {
91-
// int currentLine = lineNumber + lineOffset;
92-
93-
// qDebug() << "Trying line" << currentLine;
94-
// stringdiff::Diff diff = *it;
95-
// diffsPerLine[currentLine].append(diff);
96-
//qDebug() << "appended" << diff.operation;
97-
// QVector<stringdiff::Diff*> diffsAtLine = diffsPerLine.at(currentLine);
98-
// diffsAtLine.append(diff);
99-
// qDebug() << diffsAtLine;
100-
//
101-
// dereference and grab address of iterator.
102-
103-
104-
//// auto diffsAtLine = diffsPerLine->at(currentLine
105-
//
106-
// // int currentLine = lineNumber + lineOffset;
107-
// // if (diffsPerLine[currentLine] == NULL) { diffsPerLine[currentLine]
108-
// qDebug() << "Line " << lineNumber + lineOffset << " change " << it->operation;
109-
// }
110-
//lineNumber += lineCount;
111-
//}
11290
}
11391

11492
return diffsPerLine;
@@ -122,35 +100,23 @@ bool fileExists(QString path) {
122100
}
123101

124102

125-
/// This is an example that shows how to load a grammar file and a theme file
126-
/// manually and initialise the editor with it
127103
int main(int argc, char *argv[])
128104
{
129105
QApplication a(argc, argv);
130106

107+
QString appDataPath_;
108+
#ifdef Q_OS_MAC
109+
appDataPath_ = applicationDirPath() + "/../Resources/";
110+
#else
111+
appDataPath_ = qApp->applicationDirPath() + "/data/";
112+
#endif
113+
131114
// initialize edbee
132115
edbee::Edbee* edbee = edbee::Edbee::instance();
116+
edbee->setKeyMapPath(QString("%1%2").arg(appDataPath_).arg("keymaps"));
117+
edbee->setGrammarPath(QString("%1%2").arg(appDataPath_).arg("syntaxfiles"));
118+
edbee->setThemePath(QString("%1%2").arg(appDataPath_).arg("themes"));
133119
edbee->autoInit();
134-
135-
// TODO: Check the edbee app for a proper way to detect grammars and themes.
136-
137-
// read the grammar file
138-
edbee::TextGrammarManager* grammarManager = edbee->grammarManager();
139-
edbee::TextGrammar* grammar = grammarManager->readGrammarFile( "HTML.tmLanguage" );
140-
if( !grammar ) {
141-
qDebug() << "Grammar couldn't be loaded: " << grammarManager->lastErrorMessage();
142-
edbee->shutdown(); // call this method manuall to prevent memory leak warnings. When the exec method is called this isn't required
143-
return 1;
144-
}
145-
146-
// read the theme file
147-
edbee::TextThemeManager* themeManager = edbee->themeManager();
148-
edbee::TextTheme* theme = themeManager->readThemeFile( "Solarized (Dark).tmTheme" );
149-
if( !theme) {
150-
qDebug() << "Theme couldn't be loaded: " << themeManager->lastErrorMessage();
151-
edbee->shutdown(); // call this method manuall to prevent memory leak warnings. When the exec method is called this isn't required
152-
return 1;
153-
}
154120

155121
//string leftContent = "<html>\n\t<title>Yo</title>\n\t<body>\n\t\t<h1>Hello Left</h1>\n\t</body>\n</html>\n";
156122
//string rightContent = "<html>\n\t<body>\n\t\t<h1>Hello Right</h1>\n\t</body>\n\t<footer>fin</footer>\n</html>\n";
@@ -161,54 +127,31 @@ int main(int argc, char *argv[])
161127
string leftContent = "Mary had a little lamb,\nwhose fleece was white as snow.\n\n";
162128
string rightContent = "Mary had a little lamb,\nwhose fleece was red as snow.\n\nAnd everywhere that Mary went,\nthe lamb was sure to go.\n";
163129

164-
////
165-
//string leftContent = "asdf\nyo";
166-
//string rightContent = "asdf\nbe";
167-
168-
// if (argc > 2) {
169-
//
170-
// char* file1 = argv[1];
171-
// char* file2 = argv[2];
172-
//
173-
// if (fileExists(file1)) {
174-
// leftContent = getFileContents(file1);
175-
// }
176-
//
177-
// if (fileExists(file2)) {
178-
// rightContent = getFileContents(file2);
179-
// }
180-
//
181-
// }
130+
string leftFile = "left.txt";
131+
string rightFile = "right.txt";
132+
133+
if (argc > 2) {
134+
135+
leftFile = argv[0];
136+
rightFile = argv[1];
137+
138+
if (fileExists(QString::fromStdString(leftFile))) {
139+
leftContent = getFileContents(leftFile);
140+
}
141+
142+
if (fileExists(QString::fromStdString(rightFile))) {
143+
rightContent = getFileContents(rightFile);
144+
}
145+
146+
}
182147

183-
// get the minimal diff
184148
diff_match_patch<string> dmp;
185149
auto diffs = dmp.diff_lines(leftContent, rightContent);
186-
187150
auto deletionLookup = createDiffLookup(diffs, stringdiff::DELETE);
188-
summarizeLines(deletionLookup);
189-
190-
191151
auto insertLookup = createDiffLookup(diffs, stringdiff::INSERT);
192-
summarizeLines(insertLookup);
193-
//
194-
// auto deletions = findDiffsByLine(diffs, stringdiff::DELETE);
195-
// auto insertions = findDiffsByLine(diffs, stringdiff::INSERT);
196-
197-
// for (int i = 0; i < deletions.size(); ++i) {
198-
// qDebug() << deletions[i] << " ";
199-
// }
200-
//
201152

202-
// TODO: generate a diff object to be used by both.
203-
/*
204-
205-
Options:
206-
207-
Set a unified diff to both sides, with left interpreting differently from right.
208-
* left: '-' means red
209-
* right: '+' means green
210-
* both: '-' followed by '+' means change
211-
153+
/*
154+
212155
We need support for:
213156
214157
* Shading of line number columns?
@@ -217,20 +160,21 @@ int main(int argc, char *argv[])
217160
218161
*/
219162

163+
QFont font = QFont("Consolas", 12);
220164
// TODO: read only?
221165
edbee::TextEditorWidget left;
222-
left.textDocument()->setLanguageGrammar( grammar );
223-
left.textRenderer()->setTheme( theme );
166+
left.config()->setFont(font);
167+
left.config()->setThemeName("Oceanic Next");
168+
left.textDocument()->setLanguageGrammar(edbee::Edbee::instance()->grammarManager()->detectGrammarWithFilename(QString::fromStdString(leftFile)));
224169
left.textDocument()->setDiffLookup(deletionLookup);
225170
left.textDocument()->setText(QString::fromStdString(leftContent));
226-
// left.textDocument()->setDiffStatus(&deletions);
227-
171+
228172
edbee::TextEditorWidget right;
229-
right.textDocument()->setLanguageGrammar( grammar );
230-
right.textRenderer()->setTheme( theme );
173+
right.config()->setFont(font);
174+
right.config()->setThemeName("Oceanic Next");
175+
right.textDocument()->setLanguageGrammar(edbee::Edbee::instance()->grammarManager()->detectGrammarWithFilename(QString::fromStdString(rightFile)));
231176
right.textDocument()->setDiffLookup(insertLookup);
232177
right.textDocument()->setText(QString::fromStdString(rightContent));
233-
// right.textDocument()->setDiffStatus(&insertions);
234178

235179
QSplitter *splitter = new QSplitter();
236180
splitter->addWidget(&left);

‎diff-match-patch-cpp-stl

0 commit comments

Comments
 (0)