forked from qt-creator/qt-creator
Add common interface for text formats inside syntax highlighter
Change-Id: I87f64446161a57aea0896f68e4eafacef791969b Reviewed-by: Orgad Shaneh <orgads@gmail.com> Reviewed-by: Erik Verbruggen <erik.verbruggen@digia.com>
This commit is contained in:
@@ -40,9 +40,7 @@
|
||||
#include "pythonhighlighter.h"
|
||||
#include "lexical/pythonscanner.h"
|
||||
|
||||
#include <texteditor/basetextdocumentlayout.h>
|
||||
#include <texteditor/basetextdocument.h>
|
||||
#include <texteditor/fontsettings.h>
|
||||
#include <texteditor/texteditorconstants.h>
|
||||
|
||||
namespace PythonEditor {
|
||||
@@ -67,30 +65,26 @@ using namespace PythonEditor::Internal;
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
/// @return List that maps enum Format values to TextEditor plugin formats
|
||||
QVector<TextEditor::TextStyle> initFormatCategories()
|
||||
{
|
||||
QVector<TextEditor::TextStyle> categories(Format_FormatsAmount);
|
||||
categories[Format_Number] = TextEditor::C_NUMBER;
|
||||
categories[Format_String] = TextEditor::C_STRING;
|
||||
categories[Format_Keyword] = TextEditor::C_KEYWORD;
|
||||
categories[Format_Type] = TextEditor::C_TYPE;
|
||||
categories[Format_ClassField] = TextEditor::C_FIELD;
|
||||
categories[Format_MagicAttr] = TextEditor::C_JS_SCOPE_VAR;
|
||||
categories[Format_Operator] = TextEditor::C_OPERATOR;
|
||||
categories[Format_Comment] = TextEditor::C_COMMENT;
|
||||
categories[Format_Doxygen] = TextEditor::C_DOXYGEN_COMMENT;
|
||||
categories[Format_Whitespace] = TextEditor::C_VISUAL_WHITESPACE;
|
||||
categories[Format_Identifier] = TextEditor::C_TEXT;
|
||||
categories[Format_ImportedModule] = TextEditor::C_STRING;
|
||||
|
||||
return categories;
|
||||
}
|
||||
|
||||
/// New instance created when opening any document in editor
|
||||
PythonHighlighter::PythonHighlighter(TextEditor::BaseTextDocument *parent) :
|
||||
TextEditor::SyntaxHighlighter(parent)
|
||||
{
|
||||
static QVector<TextEditor::TextStyle> categories;
|
||||
if (categories.isEmpty()) {
|
||||
categories << TextEditor::C_NUMBER
|
||||
<< TextEditor::C_STRING
|
||||
<< TextEditor::C_KEYWORD
|
||||
<< TextEditor::C_TYPE
|
||||
<< TextEditor::C_FIELD
|
||||
<< TextEditor::C_JS_SCOPE_VAR
|
||||
<< TextEditor::C_OPERATOR
|
||||
<< TextEditor::C_COMMENT
|
||||
<< TextEditor::C_DOXYGEN_COMMENT
|
||||
<< TextEditor::C_TEXT
|
||||
<< TextEditor::C_VISUAL_WHITESPACE
|
||||
<< TextEditor::C_STRING;
|
||||
}
|
||||
setTextFormatCategories(categories);
|
||||
}
|
||||
|
||||
/// Instance destroyed when one of documents closed from editor
|
||||
@@ -98,18 +92,6 @@ PythonHighlighter::~PythonHighlighter()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
QtCreator has own fonts&color settings. Highlighter wants get access to
|
||||
this settings before highlightBlock() called first time.
|
||||
Settings provided by PyEditor::EditorWidget class.
|
||||
*/
|
||||
void PythonHighlighter::setFontSettings(const TextEditor::FontSettings &fs)
|
||||
{
|
||||
QVector<TextEditor::TextStyle> categories = initFormatCategories();
|
||||
m_formats = fs.toTextCharFormats(categories);
|
||||
rehighlight();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Highlighter::highlightBlock highlights single line of Python code
|
||||
* @param text is single line without EOLN symbol. Access to all block data
|
||||
@@ -155,13 +137,13 @@ int PythonHighlighter::highlightLine(const QString &text, int initialState)
|
||||
if (format == Format_Keyword) {
|
||||
QString value = scanner.value(tk);
|
||||
if (isImportKeyword(value) && hasOnlyWhitespace) {
|
||||
setFormat(tk.begin(), tk.length(), m_formats[format]);
|
||||
setFormat(tk.begin(), tk.length(), formatForCategory(format));
|
||||
highlightImport(scanner);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
setFormat(tk.begin(), tk.length(), m_formats[format]);
|
||||
setFormat(tk.begin(), tk.length(), formatForCategory(format));
|
||||
if (format != Format_Whitespace)
|
||||
hasOnlyWhitespace = false;
|
||||
}
|
||||
@@ -178,7 +160,7 @@ void PythonHighlighter::highlightImport(Scanner &scanner)
|
||||
Format format = tk.format();
|
||||
if (tk.format() == Format_Identifier)
|
||||
format = Format_ImportedModule;
|
||||
setFormat(tk.begin(), tk.length(), m_formats[format]);
|
||||
setFormat(tk.begin(), tk.length(), formatForCategory(format));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,8 +32,6 @@
|
||||
|
||||
#include "../pythoneditor_global.h"
|
||||
#include <texteditor/syntaxhighlighter.h>
|
||||
#include <texteditor/fontsettings.h>
|
||||
#include <QMap>
|
||||
|
||||
namespace PythonEditor {
|
||||
|
||||
@@ -46,15 +44,12 @@ public:
|
||||
explicit PythonHighlighter(TextEditor::BaseTextDocument *parent);
|
||||
virtual ~PythonHighlighter();
|
||||
|
||||
void setFontSettings(const TextEditor::FontSettings &fs);
|
||||
|
||||
protected:
|
||||
virtual void highlightBlock(const QString &text);
|
||||
|
||||
private:
|
||||
int highlightLine(const QString &text, int initialState);
|
||||
void highlightImport(Internal::Scanner &scanner);
|
||||
QVector<QTextCharFormat> m_formats;
|
||||
};
|
||||
|
||||
} // namespace PythonEditor
|
||||
|
||||
Reference in New Issue
Block a user