Browse Source

Add syntax highlighting

merge-requests/365/head
Jørgen Lien Sellæg 11 years ago
parent
commit
b2f2ccb388
  1. 1
      .gitignore
  2. 38
      juci/CMakeLists.txt
  3. 375
      juci/config.json
  4. 2
      juci/notebook.cc
  5. 64
      juci/source.cc
  6. 18
      juci/source.h
  7. 92
      juci/sourcefile.cc
  8. 23
      juci/sourcefile.h

1
.gitignore vendored

@ -8,3 +8,4 @@
!*.cc !*.cc
!*.h !*.h
!CMakeLists.txt !CMakeLists.txt
!config.json

38
juci/CMakeLists.txt

@ -5,9 +5,19 @@ set(module juci_to_python_api)
project (${project_name}) project (${project_name})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/")
INCLUDE(FindPkgConfig) INCLUDE(FindPkgConfig)
message("Searcing for libclang")
#LIBCLANG_FOUND System has libclang.
#LIBCLANG_INCLUDE_DIRS The libclang include directories.
#LIBCLANG_LIBRARIES The libraries needed to use libclang.
#LIBCLANG_LIBRARY_DIR The path to the directory containing libclang.
#LIBCLANG_KNOWN_LLVM_VERSIONS Known LLVM release numbers.
find_package(Testlcl)
#### Finding boost, the variables below is set ##### #### Finding boost, the variables below is set #####
#PYTHONLIBS_FOUND - True if headers and requested libraries were found #PYTHONLIBS_FOUND - True if headers and requested libraries were found
#PYTHON_INCLUDE_DIRS - Boost include directories #PYTHON_INCLUDE_DIRS - Boost include directories
@ -59,7 +69,6 @@ else()
message("Gtkmm link dirs ${GTKMM_LIBRARIES}") message("Gtkmm link dirs ${GTKMM_LIBRARIES}")
message(FATAL_ERROR "The gtkmm libraries are required. Quitting.") message(FATAL_ERROR "The gtkmm libraries are required. Quitting.")
endif() endif()
# name of the executable on Windows will be example.exe # name of the executable on Windows will be example.exe
add_executable(${project_name} add_executable(${project_name}
#list of every needed file to create the executable #list of every needed file to create the executable
@ -67,6 +76,8 @@ add_executable(${project_name}
keybindings keybindings
menu menu
source source
sourcefile.h
sourcefile.cc
window window
api api
notebook notebook
@ -81,11 +92,30 @@ add_library(${module} SHARED
) )
# dependencies # dependencies
include_directories(${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${GTKMM_INCLUDE_DIRS} "/home/zalox/bachelor/libclang++/")
link_directories(${GTKMM_LIBRARY_DIRS} ${Boost_LIBRARY_DIRS} ${PYTHON_INCLUDE_DIRS} "/home/zalox/bachelor/libclang++/")
if(${lcl_NOTFOUND})
message("FATAL_ERROR ohh lord!")
else()
message("Found libs!")
endif()
include_directories(
${Boost_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIRS}
${GTKMM_INCLUDE_DIRS}
"/home/zalox/bachelor/libclang++/headers/"
)
link_directories(
${GTKMM_LIBRARY_DIRS}
${Boost_LIBRARY_DIRS}
${PYTHON_INCLUDE_DIRS}
${LCL_LIBRARY_DIRS}
)
#module: #module:
set_target_properties(${module} PROPERTIES PREFIX "") set_target_properties(${module} PROPERTIES PREFIX "")
target_link_libraries(${module} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES}) target_link_libraries(${module} ${PYTHON_LIBRARIES} ${Boost_LIBRARIES})
#executable: #executable:
target_link_libraries(${project_name} ${GTKMM_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} "/home/zalox/bachelor/libclang++/") target_link_libraries(${project_name} ${LCL_LIBRARIES} ${GTKMM_LIBRARIES} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

375
juci/config.json

@ -1,192 +1,193 @@
{ {
"colors": { "colors": {
"text_color": ["black", "standard text color"], "text_color": "black",
"string": ["green", "string text color"] "string" : "green"
}, },
"syntax": { "syntax": {
"1": ["text_color","CXCursor_UnexposedDecl"], "1": "text_color",
"2": ["text_color","CXCursor_StructDecl"], "2": "text_color",
"3": ["text_color","CXCursor_UnionDecl"], "2": "text_color",
"4": ["text_color","CXCursor_ClassDecl"], "3": "text_color",
"5": ["text_color","CXCursor_EnumDecl"], "4": "text_color",
"6": ["text_color","CXCursor_FieldDecl"], "5": "text_color",
"7": ["text_color","CXCursor_EnumConstantDecl"], "6": "text_color",
"8": ["text_color","CXCursor_FunctionDecl"], "7": "text_color",
"9": ["text_color","CXCursor_VarDecl"], "8": "text_color",
"10": ["text_color","CXCursor_ParmDecl"], "9": "text_color",
"11": ["text_color","CXCursor_ObjCInterfaceDecl"], "10": "text_color",
"12": ["text_color","CXCursor_ObjCCategoryDecl"], "11": "text_color",
"13": ["text_color","CXCursor_ObjCProtocolDecl"], "12": "text_color",
"14": ["text_color","CXCursor_ObjCPropertyDecl"], "13": "text_color",
"15": ["text_color","CXCursor_ObjCIvarDecl"], "14": "text_color",
"16": ["text_color","CXCursor_ObjCInstanceMethodDecl"], "15": "text_color",
"17": ["text_color","CXCursor_ObjCClassMethodDecl"], "16": "text_color",
"18": ["text_color","CXCursor_ObjCImplementationDecl"], "17": "text_color",
"19": ["text_color","CXCursor_ObjCCategoryImplDecl"], "18": "text_color",
"20": ["text_color","CXCursor_TypedefDecl"], "19": "text_color",
"21": ["text_color","CXCursor_CXXMethod"], "20": "text_color",
"22": ["text_color","CXCursor_Namespace"], "21": "text_color",
"23": ["text_color","CXCursor_LinkageSpec"], "22": "text_color",
"24": ["text_color","CXCursor_Constructor"], "23": "text_color",
"25": ["text_color","CXCursor_Destructor"], "24": "text_color",
"26": ["text_color","CXCursor_ConversionFunction"], "25": "text_color",
"27": ["text_color","CXCursor_TemplateTypeParameter"], "26": "text_color",
"28": ["text_color","CXCursor_NonTypeTemplateParameter"], "27": "text_color",
"29": ["text_color","CXCursor_TemplateTemplateParameter"], "28": "text_color",
"30": ["text_color","CXCursor_FunctionTemplate"], "29": "text_color",
"31": ["text_color","CXCursor_ClassTemplate"], "30": "text_color",
"32": ["text_color","CXCursor_ClassTemplatePartialSpecialization"], "31": "text_color",
"33": ["text_color","CXCursor_NamespaceAlias"], "32": "text_color",
"34": ["text_color","CXCursor_UsingDirective"], "33": "text_color",
"35": ["text_color","CXCursor_UsingDeclaration"], "34": "text_color",
"36": ["text_color","CXCursor_TypeAliasDecl"], "35": "text_color",
"37": ["text_color","CXCursor_ObjCSynthesizeDecl"], "36": "text_color",
"38": ["text_color","CXCursor_ObjCDynamicDecl"], "37": "text_color",
"39": ["text_color","CXCursor_CXXAccessSpecifier"], "38": "text_color",
"40": ["text_color","CXCursor_FirstRef"], "39": "text_color",
"41": ["text_color","CXCursor_ObjCSuperClassRef"], "40": "text_color",
"42": ["text_color","CXCursor_ObjCProtocolRef"], "41": "text_color",
"43": ["text_color","CXCursor_ObjCClassRef"], "42": "text_color",
"44": ["text_color","CXCursor_TypeRef"], "43": "text_color",
"45": ["text_color","CXCursor_CXXBaseSpecifier"], "44": "text_color",
"46": ["text_color","CXCursor_TemplateRef"], "45": "text_color",
"47": ["text_color","CXCursor_NamespaceRef"], "46": "text_color",
"48": ["text_color","CXCursor_MemberRef"], "47": "text_color",
"49": ["text_color","CXCursor_LabelRef"], "48": "text_color",
"50": ["text_color","CXCursor_OverloadedDeclRef"], "49": "text_color",
"51": ["text_color","CXCursor_VariableRef"], "50": "text_color",
"70": ["text_color","CXCursor_FirstInvalid"], "51": "text_color",
"70": ["text_color","CXCursor_InvalidFile"], "70": "text_color",
"71": ["text_color","CXCursor_NoDeclFound"], "70": "text_color",
"72": ["text_color","CXCursor_NotImplemented"], "71": "text_color",
"73": ["text_color","CXCursor_InvalidCode"], "72": "text_color",
"100": ["text_color","CXCursor_FirstExpr"], "73": "text_color",
"100": ["text_color","CXCursor_UnexposedExpr"], "100": "text_color",
"101": ["text_color","CXCursor_DeclRefExpr"], "100": "text_color",
"102": ["text_color","CXCursor_MemberRefExpr"], "101": "text_color",
"103": ["text_color","CXCursor_CallExpr"], "102": "text_color",
"104": ["text_color","CXCursor_ObjCMessageExpr"], "103": "text_color",
"105": ["text_color","CXCursor_BlockExpr"], "104": "text_color",
"106": ["text_color","CXCursor_IntegerLiteral"], "105": "text_color",
"107": ["text_color","CXCursor_FloatingLiteral"], "106": "text_color",
"108": ["text_color","CXCursor_ImaginaryLiteral"], "107": "text_color",
"109": ["string","CXCursor_StringLiteral"], "108": "text_color",
"110": ["text_color","CXCursor_CharacterLiteral"], "109": "string",
"111": ["text_color","CXCursor_ParenExpr"], "110": "text_color",
"112": ["text_color","CXCursor_UnaryOperator"], "111": "text_color",
"113": ["text_color","CXCursor_ArraySubscriptExpr"], "112": "text_color",
"114": ["text_color","CXCursor_BinaryOperator"], "113": "text_color",
"115": ["text_color","CXCursor_CompoundAssignOperator"], "114": "text_color",
"116": ["text_color","CXCursor_ConditionalOperator"], "115": "text_color",
"117": ["text_color","CXCursor_CStyleCastExpr"], "116": "text_color",
"118": ["text_color","CXCursor_CompoundLiteralExpr"], "117": "text_color",
"119": ["text_color","CXCursor_InitListExpr"], "118": "text_color",
"120": ["text_color","CXCursor_AddrLabelExpr"], "119": "text_color",
"121": ["text_color","CXCursor_StmtExpr"], "120": "text_color",
"122": ["text_color","CXCursor_GenericSelectionExpr"], "121": "text_color",
"123": ["text_color","CXCursor_GNUNullExpr"], "122": "text_color",
"124": ["text_color","CXCursor_CXXStaticCastExpr"], "123": "text_color",
"125": ["text_color","CXCursor_CXXDynamicCastExpr"], "124": "text_color",
"126": ["text_color","CXCursor_CXXReinterpretCastExpr"], "125": "text_color",
"127": ["text_color","CXCursor_CXXConstCastExpr"], "126": "text_color",
"128": ["text_color","CXCursor_CXXFunctionalCastExpr"], "127": "text_color",
"129": ["text_color","CXCursor_CXXTypeidExpr"], "128": "text_color",
"130": ["text_color","CXCursor_CXXBoolLiteralExpr"], "129": "text_color",
"131": ["text_color","CXCursor_CXXNullPtrLiteralExpr"], "130": "text_color",
"132": ["text_color","CXCursor_CXXThisExpr"], "131": "text_color",
"133": ["text_color","CXCursor_CXXThrowExpr"], "132": "text_color",
"134": ["text_color","CXCursor_CXXNewExpr"], "133": "text_color",
"135": ["text_color","CXCursor_CXXDeleteExpr"], "134": "text_color",
"136": ["text_color","CXCursor_UnaryExpr"], "135": "text_color",
"137": ["text_color","CXCursor_ObjCStringLiteral"], "136": "text_color",
"138": ["text_color","CXCursor_ObjCEncodeExpr"], "137": "text_color",
"139": ["text_color","CXCursor_ObjCSelectorExpr"], "138": "text_color",
"140": ["text_color","CXCursor_ObjCProtocolExpr"], "139": "text_color",
"141": ["text_color","CXCursor_ObjCBridgedCastExpr"], "140": "text_color",
"142": ["text_color","CXCursor_PackExpansionExpr"], "141": "text_color",
"143": ["text_color","CXCursor_SizeOfPackExpr"], "142": "text_color",
"144": ["text_color","CXCursor_LambdaExpr"], "143": "text_color",
"145": ["text_color","CXCursor_ObjCBoolLiteralExpr"], "144": "text_color",
"146": ["text_color","CXCursor_ObjCSelfExpr"], "145": "text_color",
"200": ["text_color","CXCursor_FirstStmt"], "146": "text_color",
"200": ["text_color","CXCursor_UnexposedStmt"], "200": "text_color",
"201": ["text_color","CXCursor_LabelStmt"], "200": "text_color",
"202": ["text_color","CXCursor_CompoundStmt"], "201": "text_color",
"203": ["text_color","CXCursor_CaseStmt"], "202": "text_color",
"204": ["text_color","CXCursor_DefaultStmt"], "203": "text_color",
"205": ["text_color","CXCursor_IfStmt"], "204": "text_color",
"206": ["text_color","CXCursor_SwitchStmt"], "205": "text_color",
"207": ["text_color","CXCursor_WhileStmt"], "206": "text_color",
"208": ["text_color","CXCursor_DoStmt"], "207": "text_color",
"209": ["text_color","CXCursor_ForStmt"], "208": "text_color",
"210": ["text_color","CXCursor_GotoStmt"], "209": "text_color",
"211": ["text_color","CXCursor_IndirectGotoStmt"], "210": "text_color",
"212": ["text_color","CXCursor_ContinueStmt"], "211": "text_color",
"213": ["text_color","CXCursor_BreakStmt"], "212": "text_color",
"214": ["text_color","CXCursor_ReturnStmt"], "213": "text_color",
"215": ["text_color","CXCursor_GCCAsmStmt"], "214": "text_color",
"216": ["text_color","CXCursor_ObjCAtTryStmt"], "215": "text_color",
"217": ["text_color","CXCursor_ObjCAtCatchStmt"], "216": "text_color",
"218": ["text_color","CXCursor_ObjCAtFinallyStmt"], "217": "text_color",
"219": ["text_color","CXCursor_ObjCAtThrowStmt"], "218": "text_color",
"220": ["text_color","CXCursor_ObjCAtSynchronizedStmt"], "219": "text_color",
"221": ["text_color","CXCursor_ObjCAutoreleasePoolStmt"], "220": "text_color",
"222": ["text_color","CXCursor_ObjCForCollectionStmt"], "221": "text_color",
"223": ["text_color","CXCursor_CXXCatchStmt"], "222": "text_color",
"224": ["text_color","CXCursor_CXXTryStmt"], "223": "text_color",
"225": ["text_color","CXCursor_CXXForRangeStmt"], "224": "text_color",
"226": ["text_color","CXCursor_SEHTryStmt"], "225": "text_color",
"227": ["text_color","CXCursor_SEHExceptStmt"], "226": "text_color",
"228": ["text_color","CXCursor_SEHFinallyStmt"], "227": "text_color",
"229": ["text_color","CXCursor_MSAsmStmt"], "228": "text_color",
"230": ["text_color","CXCursor_NullStmt"], "229": "text_color",
"231": ["text_color","CXCursor_DeclStmt"], "230": "text_color",
"232": ["text_color","CXCursor_OMPParallelDirective"], "231": "text_color",
"233": ["text_color","CXCursor_OMPSimdDirective"], "232": "text_color",
"234": ["text_color","CXCursor_OMPForDirective"], "233": "text_color",
"235": ["text_color","CXCursor_OMPSectionsDirective"], "234": "text_color",
"236": ["text_color","CXCursor_OMPSectionDirective"], "235": "text_color",
"237": ["text_color","CXCursor_OMPSingleDirective"], "236": "text_color",
"238": ["text_color","CXCursor_OMPParallelForDirective"], "237": "text_color",
"239": ["text_color","CXCursor_OMPParallelSectionsDirective"], "238": "text_color",
"240": ["text_color","CXCursor_OMPTaskDirective"], "239": "text_color",
"241": ["text_color","CXCursor_OMPMasterDirective"], "240": "text_color",
"242": ["text_color","CXCursor_OMPCriticalDirective"], "241": "text_color",
"243": ["text_color","CXCursor_OMPTaskyieldDirective"], "242": "text_color",
"244": ["text_color","CXCursor_OMPBarrierDirective"], "243": "text_color",
"245": ["text_color","CXCursor_OMPTaskwaitDirective"], "244": "text_color",
"246": ["text_color","CXCursor_OMPFlushDirective"], "245": "text_color",
"247": ["text_color","CXCursor_SEHLeaveStmt"], "246": "text_color",
"248": ["text_color","CXCursor_OMPOrderedDirective"], "247": "text_color",
"249": ["text_color","CXCursor_OMPAtomicDirective"], "248": "text_color",
"250": ["text_color","CXCursor_OMPForSimdDirective"], "249": "text_color",
"251": ["text_color","CXCursor_OMPParallelForSimdDirective"], "250": "text_color",
"252": ["text_color","CXCursor_OMPTargetDirective"], "251": "text_color",
"253": ["text_color","CXCursor_OMPTeamsDirective"], "252": "text_color",
"300": ["text_color","CXCursor_TranslationUnit"], "253": "text_color",
"400": ["text_color","CXCursor_FirstAttr"], "300": "text_color",
"400": ["text_color","CXCursor_UnexposedAttr"], "400": "text_color",
"401": ["text_color","CXCursor_IBActionAttr"], "400": "text_color",
"402": ["text_color","CXCursor_IBOutletAttr"], "401": "text_color",
"403": ["text_color","CXCursor_IBOutletCollectionAttr"], "402": "text_color",
"404": ["text_color","CXCursor_CXXFinalAttr"], "403": "text_color",
"405": ["text_color","CXCursor_CXXOverrideAttr"], "404": "text_color",
"406": ["text_color","CXCursor_AnnotateAttr"], "405": "text_color",
"407": ["text_color","CXCursor_AsmLabelAttr"], "406": "text_color",
"408": ["text_color","CXCursor_PackedAttr"], "407": "text_color",
"409": ["text_color","CXCursor_PureAttr"], "408": "text_color",
"410": ["text_color","CXCursor_ConstAttr"], "409": "text_color",
"411": ["text_color","CXCursor_NoDuplicateAttr"], "410": "text_color",
"412": ["text_color","CXCursor_CUDAConstantAttr"], "411": "text_color",
"413": ["text_color","CXCursor_CUDADeviceAttr"], "412": "text_color",
"414": ["text_color","CXCursor_CUDAGlobalAttr"], "413": "text_color",
"415": ["text_color","CXCursor_CUDAHostAttr"], "414": "text_color",
"416": ["text_color","CXCursor_CUDASharedAttr"], "415": "text_color",
"500": ["text_color","CXCursor_PreprocessingDirective"], "416": "text_color",
"501": ["text_color","CXCursor_MacroDefinition"], "500": "text_color",
"502": ["text_color","CXCursor_MacroExpansion"], "501": "text_color",
"503": ["text_color","CXCursor_InclusionDirective"], "502": "text_color",
"600": ["text_color","CXCursor_ModuleImportDecl"], "503": "text_color",
"700": ["text_color","CXCursor_OverloadCandidate"] "600": "text_color",
} "700": "text_color"
}
} }

2
juci/notebook.cc

@ -14,6 +14,7 @@ Notebook::Controller::Controller(Keybindings::Controller& keybindings){
scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow());
source_vec_.push_back(new Source::Controller); source_vec_.push_back(new Source::Controller);
scrolledwindow_vec_.back()->add(source_vec_.back()->view()); scrolledwindow_vec_.back()->add(source_vec_.back()->view());
source_vec_.back()->OnNewEmptyFile();
view_.notebook().append_page(*scrolledwindow_vec_.back(), "juCi++"); view_.notebook().append_page(*scrolledwindow_vec_.back(), "juCi++");
@ -71,6 +72,7 @@ void Notebook::Controller::OnNewPage(std::string name) {
scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow()); scrolledwindow_vec_.push_back(new Gtk::ScrolledWindow());
source_vec_.push_back(new Source::Controller); source_vec_.push_back(new Source::Controller);
scrolledwindow_vec_.back()->add(source_vec_.back()->view()); scrolledwindow_vec_.back()->add(source_vec_.back()->view());
source_vec_.back()->OnNewEmptyFile();
view_.notebook().append_page(*scrolledwindow_vec_.back(), name); view_.notebook().append_page(*scrolledwindow_vec_.back(), name);
view_.notebook().show_all_children(); view_.notebook().show_all_children();
view_.notebook().set_focus_child(*scrolledwindow_vec_.back()); view_.notebook().set_focus_child(*scrolledwindow_vec_.back());

64
juci/source.cc

@ -1,5 +1,8 @@
#include "source.h" #include "source.h"
#include <iostream> #include <iostream>
#include "sourcefile.h"
#include <boost/property_tree/json_parser.hpp>
#include <fstream>
////////////// //////////////
//// View //// //// View ////
@ -12,11 +15,10 @@ Source::View::View() {
string Source::View::UpdateLine() { string Source::View::UpdateLine() {
Gtk::TextIter line(get_buffer()->get_insert()->get_iter()); Gtk::TextIter line(get_buffer()->get_insert()->get_iter());
// for each word --> check what it is --> apply appropriate tag // for each word --> check what it is --> apply appropriate tag
return "";
// retUrn "";
} }
String Source::View::Getline(const Gtk::TextIter &begin) { string Source::View::GetLine(const Gtk::TextIter &begin) {
Gtk::TextIter end(begin); Gtk::TextIter end(begin);
while (!end.ends_line()) while (!end.ends_line())
end++; end++;
@ -31,12 +33,28 @@ void Source::View::ApplyTheme(const Source::Theme &theme) {
} }
} }
void Source::View::OnOpenFile(std::vector<Clang::SourceLocation> &locations,
const Source::Theme &theme) {
Glib::RefPtr<Gtk::TextBuffer> buffer = get_buffer();
for (auto &loc : locations) {
string type = std::to_string(loc.kind());
int linum = loc.line_number();
int begin = loc.begin();
int end = loc.end();
buffer->apply_tag_by_name(theme.tagtable().at(type),
buffer->get_iter_at_line_offset(linum, begin),
buffer->get_iter_at_line_offset(linum, end));
}
}
// Source::View::Theme::tagtable() // Source::View::Theme::tagtable()
// returns a const refrence to the tagtable // returns a const refrence to the tagtable
const std::unordered_map<string, string>& Source::Theme::tagtable() const { const std::unordered_map<string, string>& Source::Theme::tagtable() const {
return tagtable_; return tagtable_;
} }
void Source::Theme::InsertTag(const string &key, const string &value) {
tagtable_[key] = value;
}
// Source::View::Theme::SetTagTable() // Source::View::Theme::SetTagTable()
// sets the tagtable for the view // sets the tagtable for the view
void Source::Theme::SetTagTable( void Source::Theme::SetTagTable(
@ -47,14 +65,37 @@ void Source::Theme::SetTagTable(
/////////////// ///////////////
//// Model //// //// Model ////
/////////////// ///////////////
Source::Model::Model() { Source::Model::Model() :
theme_() {
std::cout << "Model constructor run" << std::endl; std::cout << "Model constructor run" << std::endl;
boost::property_tree::ptree pt;
boost::property_tree::json_parser::read_json("config.json", pt);
for ( auto &i : pt ) {
boost::property_tree::ptree props = pt.get_child(i.first);
for (auto &pi : props) {
if (i.first.compare("syntax")) { // checks the config-file
theme_.InsertTag(pi.first, pi.second.get_value<std::string>());
}
}
}
}
Source::Theme& Source::Model::theme() {
return theme_;
} }
const string Source::Model::filepath() { const string Source::Model::filepath() {
return filepath_; return filepath_;
} }
void Source::Model::SetFilePath(const string &filepath) {
filepath_ = filepath;
}
void Source::Model::
SetSourceLocations(const std::vector<Clang::SourceLocation> &locations) {
locations_ = locations;
}
//////////////////// ////////////////////
//// Controller //// //// Controller ////
//////////////////// ////////////////////
@ -82,3 +123,18 @@ Source::Model& Source::Controller::model() {
void Source::Controller::OnLineEdit() { void Source::Controller::OnLineEdit() {
view().UpdateLine(); view().UpdateLine();
} }
void Source::Controller::OnNewEmptyFile() {
string filename("/tmp/juci_t");
sourcefile s(filename);
model().SetFilePath(filename);
s.save("");
}
void Source::Controller::OnOpenFile(const string &filename) {
sourcefile s(filename);
view().get_buffer()->set_text(s.get_content());
Clang::TranslationUnit tu(filename.c_str());
model().SetSourceLocations(tu.getSourceLocations());
view().OnOpenFile(model().getSourceLocations(), model().theme());
}

18
juci/source.h

@ -5,6 +5,7 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include "gtkmm.h" #include "gtkmm.h"
#include <TranslationUnit.h>
using std::string; using std::string;
@ -13,6 +14,7 @@ namespace Source {
public: public:
const std::unordered_map<string, string>& tagtable() const; const std::unordered_map<string, string>& tagtable() const;
void SetTagTable(const std::unordered_map<string, string> &tagtable); void SetTagTable(const std::unordered_map<string, string> &tagtable);
void InsertTag(const string &key, const string &value);
private: private:
std::unordered_map<string, string> tagtable_; std::unordered_map<string, string> tagtable_;
string background_; string background_;
@ -23,6 +25,8 @@ namespace Source {
View(); View();
string UpdateLine(); string UpdateLine();
void ApplyTheme(const Theme &theme); void ApplyTheme(const Theme &theme);
void OnOpenFile(std::vector<Clang::SourceLocation> &locations,
const Theme &theme);
private: private:
string GetLine(const Gtk::TextIter &begin); string GetLine(const Gtk::TextIter &begin);
}; // class View }; // class View
@ -30,11 +34,18 @@ namespace Source {
class Model{ class Model{
public: public:
Model(); Model();
Theme theme(); Theme& theme();
const string filepath(); const string filepath();
void SetFilePath(const string &filepath);
void SetSourceLocations(const std::vector<Clang::SourceLocation> &locations);
std::vector<Clang::SourceLocation>& getSourceLocations() {
return locations_;
}
private: private:
Theme theme_; Theme theme_;
string filepath_; string filepath_;
std::vector<Clang::SourceLocation> locations_;
}; };
class Controller { class Controller {
@ -42,12 +53,13 @@ namespace Source {
Controller(); Controller();
View& view(); View& view();
Model& model(); Model& model();
void OnNewEmptyFile();
void OnOpenFile(const string &filename);
private: private:
void OnLineEdit(); void OnLineEdit();
void OnOpenFile();
void OnSaveFile(); void OnSaveFile();
protected: protected:
View view_; View view_;
Model model_; Model model_;

92
juci/sourcefile.cc

@ -0,0 +1,92 @@
#include "sourcefile.h"
#include <giomm.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
sourcefile::sourcefile(const string &input_filename)
: lines(), filename(input_filename) {
open(input_filename);
}
/**
*
*/
void sourcefile::open(const string &filename) {
Gio::init();
// Creates/Opens a file specified by the input string.
Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(filename);
if (!file) // Gio::File has overloaded operator
cerr << "Was not able to open file: " << filename << endl;
// Creates pointer for filestream
if (!file->query_exists()) {
file->create_file()->close();
}
Glib::RefPtr<Gio::FileInputStream> stream = file->read();
if (!stream) // error message on stream failure
cerr << filename << " returned an empty stream" << endl;
Glib::RefPtr<Gio::DataInputStream>
datainput = Gio::DataInputStream::create(stream);
string line;
while (datainput->read_line(line)) {
lines.push_back(line);
}
datainput->close();
stream->close();
}
vector<string> sourcefile::get_lines() {
return lines;
}
string sourcefile::get_line(int line_number) {
return lines[line_number];
}
int sourcefile::save(const string &text) {
Gio::init();
// Creates/Opens a file specified by the input string.
Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(filename);
if (!file) // Gio::File has overloaded operator
cerr << "Was not able to open file: " << filename << endl;
// Creates
Glib::RefPtr<Gio::FileOutputStream> stream =
file->query_exists() ? file->replace() : file->create_file();
if (!stream) // error message on stream failure
cerr << filename << " returned an empty stream" << endl;
Glib::RefPtr<Gio::DataOutputStream>
output = Gio::DataOutputStream::create(stream);
output->put_string(text);
output->close();
stream->close();
return 0;
}
string sourcefile::get_content() {
string res;
for (auto line : lines) {
res.append(line).append("\n");
}
return res;
}

23
juci/sourcefile.h

@ -0,0 +1,23 @@
#ifndef JUCI_SOURCEFILE_H_
#define JUCI_SOURCEFILE_H_
#include <string>
#include <vector>
using namespace std;
class sourcefile {
public:
explicit sourcefile(const string &filename);
vector<string> get_lines();
string get_content();
string get_line(int line_number);
int save(const string &text);
private:
void open(const string &filename);
vector<string> lines;
string filename;
};
#endif // JUCI_SOURCEFILE_H_
Loading…
Cancel
Save