2013-01-14 23:11:10 +04:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2013-01-14 23:11:10 +04:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2013-01-14 23:11:10 +04:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2013-01-14 23:11:10 +04:00
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief The Highlighter class pre-highlights Python source using simple scanner.
|
|
|
|
|
*
|
|
|
|
|
* Highlighter doesn't highlight user types (classes and enumerations), syntax
|
|
|
|
|
* and semantic errors, unnecessary code, etc. It's implements only
|
|
|
|
|
* basic highlight mechanism.
|
|
|
|
|
*
|
|
|
|
|
* Main highlight procedure is highlightBlock().
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "pythonhighlighter.h"
|
2016-08-03 00:07:44 +02:00
|
|
|
#include "pythonscanner.h"
|
2013-01-14 23:11:10 +04:00
|
|
|
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/textdocument.h>
|
2013-01-14 23:11:10 +04:00
|
|
|
#include <texteditor/texteditorconstants.h>
|
|
|
|
|
|
|
|
|
|
namespace PythonEditor {
|
2014-08-27 13:38:02 +02:00
|
|
|
namespace Internal {
|
2013-01-14 23:11:10 +04:00
|
|
|
|
|
|
|
|
/**
|
2014-08-27 16:19:55 +02:00
|
|
|
* @class PythonEditor::Internal::PythonHighlighter
|
2013-01-14 23:11:10 +04:00
|
|
|
* @brief Handles incremental lexical highlighting, but not semantic
|
|
|
|
|
*
|
|
|
|
|
* Incremental lexical highlighting works every time when any character typed
|
|
|
|
|
* or some text inserted (i.e. copied & pasted).
|
|
|
|
|
* Each line keeps associated scanner state - integer number. This state is the
|
|
|
|
|
* scanner context for next line. For example, 3 quotes begin a multiline
|
|
|
|
|
* string, and each line up to next 3 quotes has state 'MultiLineString'.
|
|
|
|
|
*
|
|
|
|
|
* @code
|
|
|
|
|
* def __init__: # Normal
|
|
|
|
|
* self.__doc__ = """ # MultiLineString (next line is inside)
|
|
|
|
|
* banana # MultiLineString
|
|
|
|
|
* """ # Normal
|
|
|
|
|
* @endcode
|
|
|
|
|
*/
|
|
|
|
|
|
2014-08-27 13:38:02 +02:00
|
|
|
PythonHighlighter::PythonHighlighter()
|
2013-01-14 23:11:10 +04:00
|
|
|
{
|
2016-08-03 00:07:44 +02:00
|
|
|
static const QVector<TextEditor::TextStyle> 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
|
|
|
|
|
};
|
2013-08-13 12:57:31 +02:00
|
|
|
setTextFormatCategories(categories);
|
2013-01-14 23:11:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-08-27 16:19:55 +02:00
|
|
|
* @brief PythonHighlighter::highlightBlock highlights single line of Python code
|
2013-01-14 23:11:10 +04:00
|
|
|
* @param text is single line without EOLN symbol. Access to all block data
|
2013-10-07 13:34:40 +02:00
|
|
|
* can be obtained through inherited currentBlock() function.
|
2013-01-14 23:11:10 +04:00
|
|
|
*
|
2013-10-07 13:34:40 +02:00
|
|
|
* This function receives state (int number) from previously highlighted block,
|
2013-01-14 23:11:10 +04:00
|
|
|
* scans block using received state and sets initial highlighting for current
|
|
|
|
|
* block. At the end, it saves internal state in current block.
|
|
|
|
|
*/
|
|
|
|
|
void PythonHighlighter::highlightBlock(const QString &text)
|
|
|
|
|
{
|
|
|
|
|
int initialState = previousBlockState();
|
|
|
|
|
if (initialState == -1)
|
|
|
|
|
initialState = 0;
|
|
|
|
|
setCurrentBlockState(highlightLine(text, initialState));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return True if this keyword is acceptable at start of import line
|
|
|
|
|
*/
|
2014-08-27 13:38:02 +02:00
|
|
|
static bool isImportKeyword(const QString &keyword)
|
2013-01-14 23:11:10 +04:00
|
|
|
{
|
2016-08-03 00:07:44 +02:00
|
|
|
return keyword == "import" || keyword == "from";
|
2013-01-14 23:11:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Highlight line of code, returns new block state
|
|
|
|
|
* @param text Source code to highlight
|
|
|
|
|
* @param initialState Initial state of scanner, retrieved from previous block
|
|
|
|
|
* @return Final state of scanner, should be saved with current block
|
|
|
|
|
*/
|
|
|
|
|
int PythonHighlighter::highlightLine(const QString &text, int initialState)
|
|
|
|
|
{
|
|
|
|
|
Scanner scanner(text.constData(), text.size());
|
|
|
|
|
scanner.setState(initialState);
|
|
|
|
|
|
|
|
|
|
FormatToken tk;
|
|
|
|
|
bool hasOnlyWhitespace = true;
|
|
|
|
|
while ((tk = scanner.read()).format() != Format_EndOfBlock) {
|
|
|
|
|
Format format = tk.format();
|
|
|
|
|
if (format == Format_Keyword) {
|
|
|
|
|
QString value = scanner.value(tk);
|
|
|
|
|
if (isImportKeyword(value) && hasOnlyWhitespace) {
|
2013-08-13 12:57:31 +02:00
|
|
|
setFormat(tk.begin(), tk.length(), formatForCategory(format));
|
2013-01-14 23:11:10 +04:00
|
|
|
highlightImport(scanner);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 12:57:31 +02:00
|
|
|
setFormat(tk.begin(), tk.length(), formatForCategory(format));
|
2013-01-14 23:11:10 +04:00
|
|
|
if (format != Format_Whitespace)
|
|
|
|
|
hasOnlyWhitespace = false;
|
|
|
|
|
}
|
|
|
|
|
return scanner.state();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Highlights rest of line as import directive
|
|
|
|
|
*/
|
|
|
|
|
void PythonHighlighter::highlightImport(Scanner &scanner)
|
|
|
|
|
{
|
|
|
|
|
FormatToken tk;
|
|
|
|
|
while ((tk = scanner.read()).format() != Format_EndOfBlock) {
|
|
|
|
|
Format format = tk.format();
|
|
|
|
|
if (tk.format() == Format_Identifier)
|
|
|
|
|
format = Format_ImportedModule;
|
2013-08-13 12:57:31 +02:00
|
|
|
setFormat(tk.begin(), tk.length(), formatForCategory(format));
|
2013-01-14 23:11:10 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-27 13:38:02 +02:00
|
|
|
} // namespace Internal
|
2013-01-14 23:11:10 +04:00
|
|
|
} // namespace PythonEditor
|