changeset 23236:4cd5f975d26c

automatically add a comment string when line breaking in line comments * file-editor-tab.cc (handle_char_added): insert a comment string at the beginning of a new line when breaking a comment line * octave-qscintilla.cc: include headers of the used qscintilla lexers; (get_style): new function providing the style at specific/current position; (is_style_comment): new function for detecting lin or block comment styles; * octave-qscintilla.h: new functions get_style and is_style_comment, new enum for return values of is_style_comment
author Torsten <mttl@mailbox.org>
date Sat, 25 Feb 2017 21:13:35 +0100
parents a36d3015f8c8
children 904c8a356e76
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/octave-qscintilla.cc libgui/src/m-editor/octave-qscintilla.h
diffstat 3 files changed, 86 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Sat Feb 25 20:54:02 2017 +0100
+++ b/libgui/src/m-editor/file-editor-tab.cc	Sat Feb 25 21:13:35 2017 +0100
@@ -2708,8 +2708,14 @@
     int col_newline = col - 1;
     if (c == ' ' || c == '\t')
       col_newline = col_space + 1;
-    // Insert a newline char for breaking the line
-    _edit_area->insertAt (QString ("\n"), line, col_newline);
+
+    // Insert a newline char for breaking the line possibly followed
+    // by a line comment string
+    QString newline = QString ("\n");
+    int style = _edit_area->is_style_comment ();
+    if (style == octave_qscintilla::ST_LINE_COMMENT)
+      newline = newline + _edit_area->comment_string ();
+    _edit_area->insertAt (newline, line, col_newline);
 
     // Automatically indent the new line to the indentation of previous line
     // and set the cursor position to the end of the indentation.
--- a/libgui/src/m-editor/octave-qscintilla.cc	Sat Feb 25 20:54:02 2017 +0100
+++ b/libgui/src/m-editor/octave-qscintilla.cc	Sat Feb 25 21:13:35 2017 +0100
@@ -28,13 +28,21 @@
 
 #if defined (HAVE_QSCINTILLA)
 
+#include <Qsci/qscilexer.h>
+
 #if defined (HAVE_QSCI_QSCILEXEROCTAVE_H)
 #  define HAVE_LEXER_OCTAVE 1
+#  include <Qsci/qscilexeroctave.h>
 #elif defined (HAVE_QSCI_QSCILEXERMATLAB_H)
 #  define HAVE_LEXER_MATLAB 1
+#  include <Qsci/qscilexermatlab.h>
 #endif
+#include <Qsci/qscilexercpp.h>
+#include <Qsci/qscilexerbash.h>
+#include <Qsci/qscilexerperl.h>
+#include <Qsci/qscilexerbatch.h>
+#include <Qsci/qscilexerdiff.h>
 
-#include <Qsci/qscilexer.h>
 #include <Qsci/qscicommandset.h>
 #include <QShortcut>
 #include <QMessageBox>
@@ -400,6 +408,66 @@
   clearIndicatorRange (0, 0, end_line, end_col, indicator_style);
 }
 
+// provide the style at a specific position
+int
+octave_qscintilla::get_style (int pos)
+{
+  int position;
+  if (pos < 0)
+    // The positition has to be reduced by 2 for getting the real style (?)
+    position = SendScintilla (QsciScintillaBase::SCI_GETCURRENTPOS) - 2;
+  else
+    position = pos;
+
+  return SendScintilla (QsciScintillaBase::SCI_GETSTYLEAT, position);
+}
+
+// Is a specific cursor position in a line or block comment?
+int
+octave_qscintilla::is_style_comment (int pos)
+{
+  int lexer = SendScintilla (QsciScintillaBase::SCI_GETLEXER);
+  int style = get_style (pos);
+
+  switch (lexer)
+    {
+      case SCLEX_CPP:
+        return (ST_LINE_COMMENT * (
+                          style == QsciLexerCPP::CommentLine
+                       || style == QsciLexerCPP::CommentLineDoc)
+              + ST_BLOCK_COMMENT * (
+                           style == QsciLexerCPP::Comment
+                        || style == QsciLexerCPP::CommentDoc
+                        || style == QsciLexerCPP::CommentDocKeyword
+                        || style == QsciLexerCPP::CommentDocKeywordError)
+                );
+
+#if defined (HAVE_LEXER_MATLAB)
+      case SCLEX_MATLAB:
+        return (ST_LINE_COMMENT * (style == QsciLexerMatlab::Comment));
+#endif
+#if  defined (HAVE_LEXER_OCTAVE)
+      case SCLEX_OCTAVE:
+        return (ST_LINE_COMMENT * (style == QsciLexerOctave::Comment));
+#endif
+
+      case SCLEX_PERL:
+        return (ST_LINE_COMMENT * (style == QsciLexerPerl::Comment));
+
+      case SCLEX_BATCH:
+        return (ST_LINE_COMMENT * (style == QsciLexerBatch::Comment));
+
+      case SCLEX_DIFF:
+        return (ST_LINE_COMMENT * (style == QsciLexerDiff::Comment));
+
+      case SCLEX_BASH:
+        return (ST_LINE_COMMENT * (style == QsciLexerBash::Comment));
+
+    }
+
+  return ST_NONE;
+}
+
 // Function returning the true cursor position where the tab length
 // is taken into account.
 void
--- a/libgui/src/m-editor/octave-qscintilla.h	Sat Feb 25 20:54:02 2017 +0100
+++ b/libgui/src/m-editor/octave-qscintilla.h	Sat Feb 25 21:13:35 2017 +0100
@@ -38,6 +38,13 @@
   octave_qscintilla (QWidget *p);
   ~octave_qscintilla ();
 
+  enum
+    {
+      ST_NONE = 0,
+      ST_LINE_COMMENT,
+      ST_BLOCK_COMMENT
+    };
+
   virtual void contextMenuEvent (QContextMenuEvent *e);
 
   void context_help_doc (bool);
@@ -48,6 +55,8 @@
   void clear_indicator (int indicator_style);
   void get_current_position (int *pos, int *line, int *col);
   QString comment_string ();
+  int get_style (int pos = -1);
+  int is_style_comment (int pos = -1);
 
 signals: