changeset 14965:db3c84d38345 gui

Now supporting c/cc/cpp syntax highlighting. * file-editor-tab: Added updating lexer dependent on the filename suffix.. * file-editor: Removed initializig a single lexer for all editor tabs.
author Jacob Dawid <jacob.dawid@gmail.com>
date Tue, 17 Jul 2012 11:38:54 -0400
parents 3a05cb67dea5
children 355d6c165df0
files gui/src/m-editor/file-editor-tab.cc gui/src/m-editor/file-editor-tab.h gui/src/m-editor/file-editor.cc gui/src/m-editor/file-editor.h
diffstat 4 files changed, 58 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/gui/src/m-editor/file-editor-tab.cc
+++ b/gui/src/m-editor/file-editor-tab.cc
@@ -18,6 +18,8 @@
 #include "file-editor-tab.h"
 #include "file-editor.h"
 #include "octave-link.h"
+
+#include "resource-manager.h"
 #include <QMessageBox>
 #include <QVBoxLayout>
 
@@ -28,7 +30,6 @@
   _file_editor = fileEditor;
   _file_name = "";
   _edit_area = new QsciScintilla (this);
-  _edit_area->setLexer (fileEditor->lexer ());
 
   // symbols
   _edit_area->setMarginType (1, QsciScintilla::SymbolMargin);
@@ -169,6 +170,7 @@
 file_editor_tab::set_file_name (QString fileName)
 {
   _file_name = fileName;
+  update_lexer ();
   update_tracked_file ();
 }
 
@@ -202,6 +204,55 @@
 }
 
 void
+file_editor_tab::update_lexer ()
+{
+  QsciLexer *lexer =  _edit_area->lexer ();
+  delete lexer;
+
+  if (_file_name.endsWith (".m") || _file_name.endsWith (".M"))
+    {
+      lexer = new lexer_octave_gui ();
+
+      // The API info that is used for auto completion
+      // TODO: Where to store a file with API info (raw or prepared?)?
+      // TODO: Also provide infos on octave-forge functions?
+      // TODO: Also provide infos on function parameters?
+      // By now, use the keywords-list from syntax highlighting
+
+      QsciAPIs *lexer_api = new QsciAPIs (lexer);
+
+      QString keyword;
+      QStringList keywordList;
+      keyword = lexer->keywords (1);  // get whole string with all keywords
+      keywordList = keyword.split (QRegExp ("\\s+"));  // split into single strings
+      int i;
+      for (i = 0; i < keywordList.size (); i++)
+        {
+          lexer_api->add (keywordList.at (i));  // add single strings to the API
+        }
+      lexer_api->prepare ();           // prepare API info ... this make take some time
+    }
+  else if (_file_name.endsWith (".c") || _file_name.endsWith (".cc") || _file_name.endsWith (".cpp"))
+    {
+      lexer = new QsciLexerCPP ();
+    }
+
+  QSettings *settings = resource_manager::instance ()->get_settings ();
+
+  // Editor font (default or from settings)
+  lexer->setDefaultFont (QFont (
+                             settings->value ("editor/fontName","Courier").toString (),
+                             settings->value ("editor/fontSize",10).toInt ()));
+
+  // TODO: Autoindent not working as it should
+  lexer->setAutoIndentStyle (QsciScintilla::AiMaintain ||
+                               QsciScintilla::AiOpening  ||
+                               QsciScintilla::AiClosing);
+
+  _edit_area->setLexer (lexer);
+}
+
+void
 file_editor_tab::request_add_breakpoint (int line)
 {
   QFileInfo file_info (_file_name);
--- a/gui/src/m-editor/file-editor-tab.h
+++ b/gui/src/m-editor/file-editor-tab.h
@@ -23,6 +23,11 @@
 #include <QCloseEvent>
 #include <QFileSystemWatcher>
 
+#include <Qsci/qsciapis.h>
+// Not available in the Debian repos yet!
+// #include <Qsci/qscilexeroctave.h>
+#include "lexer-octave-gui.h"
+#include <Qsci/qscilexercpp.h>
 #include "octave-event-observer.h"
 
 class file_editor;
@@ -79,6 +84,7 @@
   void set_file_name (QString fileName);
 
 private:
+  void update_lexer ();
   void request_add_breakpoint (int line);
   void request_remove_breakpoint (int line);
 
--- a/gui/src/m-editor/file-editor.cc
+++ b/gui/src/m-editor/file-editor.cc
@@ -39,12 +39,6 @@
 {
 }
 
-lexer_octave_gui *
-file_editor::lexer ()
-{
-  return _lexer;
-}
-
 QTerminal *
 file_editor::terminal ()
 {
@@ -475,36 +469,6 @@
   connect (_tab_widget, SIGNAL (tabCloseRequested (int)), this, SLOT (handle_tab_close_request (int)));
   connect (_tab_widget, SIGNAL (currentChanged(int)), this, SLOT (active_tab_changed (int)));
 
-  // this has to be done only once, not for each editor
-  _lexer = new lexer_octave_gui ();
-
-  // Editor font (default or from settings)
-  _lexer->setDefaultFont (QFont (
-                             settings->value ("editor/fontName","Courier").toString (),
-                             settings->value ("editor/fontSize",10).toInt ()));
-
-  // TODO: Autoindent not working as it should
-  _lexer->setAutoIndentStyle (QsciScintilla::AiMaintain ||
-                               QsciScintilla::AiOpening  ||
-                               QsciScintilla::AiClosing);
-
-  // The API info that is used for auto completion
-  // TODO: Where to store a file with API info (raw or prepared?)?
-  // TODO: Also provide infos on octave-forge functions?
-  // TODO: Also provide infos on function parameters?
-  // By now, use the keywords-list from syntax highlighting
-  _lexer_api = new QsciAPIs (_lexer);
-
-  QString keyword;
-  QStringList keywordList;
-  keyword = _lexer->keywords (1);  // get whole string with all keywords
-  keywordList = keyword.split (QRegExp ("\\s+"));  // split into single strings
-  int i;
-  for (i = 0; i < keywordList.size (); i++)
-    {
-      _lexer_api->add (keywordList.at (i));  // add single strings to the API
-    }
-  _lexer_api->prepare ();           // prepare API info ... this make take some time
   resize (500, 400);
   setWindowIcon (QIcon::fromTheme ("accessories-text-editor", style->standardIcon (QStyle::SP_FileIcon)));
   setWindowTitle ("Octave Editor");
--- a/gui/src/m-editor/file-editor.h
+++ b/gui/src/m-editor/file-editor.h
@@ -28,10 +28,6 @@
 #include <QStatusBar>
 #include <QCloseEvent>
 #include <QTabWidget>
-#include <Qsci/qsciapis.h>
-// Not available in the Debian repos yet!
-// #include <Qsci/qscilexeroctave.h>
-#include "lexer-octave-gui.h"
 
 const char UNNAMED_FILE[]     = "<unnamed>";
 const char SAVE_FILE_FILTER[] = "Octave Files (*.m);;All Files (*.*)";
@@ -51,7 +47,6 @@
   ~file_editor ();
   void loadFile (QString fileName);
 
-  lexer_octave_gui *lexer ();
   QTerminal *       terminal ();
   main_window *     get_main_window ();
 
@@ -105,8 +100,6 @@
   QAction *         _run_action;
   QTabWidget *      _tab_widget;
   int               _marker_breakpoint;
-  lexer_octave_gui *_lexer;
-  QsciAPIs *        _lexer_api;
 };
 
 #endif // FILEEDITORMDISUBWINDOW_H