23#include <Qsci/qscilexerhtml.h>
64 QsciLexerHTML *lexer =
new QsciLexerHTML(
this );
65 lexer->setDefaultFont( font );
68 lexer->setFont( font, -1 );
70 font.setItalic(
true );
71 lexer->setFont( font, QsciLexerHTML::HTMLComment );
72 lexer->setFont( font, QsciLexerHTML::JavaScriptComment );
73 lexer->setFont( font, QsciLexerHTML::JavaScriptCommentLine );
102 QString newText = string;
104 const QString definePrettify = QStringLiteral(
105 "def __qgis_prettify(text):\n"
107 " from bs4 import BeautifulSoup\n"
108 " return BeautifulSoup(text, 'html.parser').prettify()\n"
109 " except ImportError:\n"
112 " from lxml import etree, html\n"
113 " text = re.sub('>\\\\s+<', '><', text)\n"
114 " text = re.sub('\\n\\\\s*', '', text)\n"
115 " document_root = html.fromstring(text)\n"
116 " return etree.tostring(document_root, encoding='utf-8', pretty_print=True).decode('utf-8')\n"
117 " except ImportError:\n"
118 " return '_ImportError'\n" );
123 QgsDebugError( QStringLiteral(
"Error running script: %1" ).arg( definePrettify ) );
131 if ( result == QLatin1String(
"_ImportError" ) )
133 showMessage( tr(
"Reformat Code" ), tr(
"HTML reformatting requires bs4 or lxml python modules to be installed" ), Qgis::MessageLevel::Warning );
142 QgsDebugError( QStringLiteral(
"Error running script: %1" ).arg( script ) );
156 const QString commentStart(
"<!--" );
157 const QString commentEnd(
"-->" );
159 int startLine, startPos, endLine, endPos;
160 if ( hasSelectedText() )
162 getSelection( &startLine, &startPos, &endLine, &endPos );
166 getCursorPosition( &startLine, &startPos );
173 while ( text( startLine ).trimmed().isEmpty() )
176 if ( startLine > endLine )
182 while ( text( endLine ).trimmed().isEmpty() )
188 QString startLineTrimmed = text( startLine );
189 startLineTrimmed.remove( QRegularExpression(
"^\\s+" ) );
191 QString endLineTrimmed = text( endLine );
192 endLineTrimmed.remove( QRegularExpression(
"\\s+$" ) );
194 const bool commented = startLineTrimmed.startsWith( commentStart ) && endLineTrimmed.endsWith( commentEnd );
197 if ( commented && startLine == endLine && text( endLine ).trimmed().size() < commentStart.size() + commentEnd.size() )
210 c2 = endLineTrimmed.size();
211 if ( endLineTrimmed.endsWith( QStringLiteral(
" " ) + commentEnd ) )
213 c1 = c2 - commentEnd.size() - 1;
217 c1 = c2 - commentEnd.size();
220 setSelection( endLine, c1, endLine, c2 );
221 removeSelectedText();
224 c1 = indentation( startLine );
225 if ( startLineTrimmed.startsWith( commentStart + QStringLiteral(
" " ) ) )
227 c2 = c1 + commentStart.size() + 1;
231 c2 = c1 + commentStart.size();
234 setSelection( startLine, c1, startLine, c2 );
235 removeSelectedText();
240 insertAt( QStringLiteral(
" " ) + commentEnd, endLine, endLineTrimmed.size() );
241 insertAt( commentStart + QStringLiteral(
" " ), startLine, indentation( startLine ) );
247 setSelection( startLine, startPos, endLine, endPos );
@ Reformat
Language supports automatic code reformatting.
@ ToggleComment
Language supports comment toggling.
ScriptLanguage
Scripting languages.
QFlags< ScriptLanguageCapability > ScriptLanguageCapabilities
Script language capabilities.
@ DoubleQuote
Double quote color.
@ CommentLine
Line comment color.
@ Default
Default text color.
@ Background
Background color.
@ SingleQuote
Single quote color.
Qgis::ScriptLanguage language() const override
Returns the associated scripting language.
Qgis::ScriptLanguageCapabilities languageCapabilities() const override
Returns the associated scripting language capabilities.
void initializeLexer() override
Called when the dialect specific code lexer needs to be initialized (or reinitialized).
QString reformatCodeString(const QString &string) override
Applies code reformatting to a string and returns the result.
QgsCodeEditorHTML(QWidget *parent=nullptr)
Constructor for QgsCodeEditorHTML.
void toggleComment() override
Toggle comment for the selected text.
A text editor based on QScintilla2.
void runPostLexerConfigurationTasks()
Performs tasks which must be run after a lexer has been set for the widget.
virtual void showMessage(const QString &title, const QString &message, Qgis::MessageLevel level)
Shows a user facing message (eg a warning message).
void setTitle(const QString &title)
Set the widget title.
Flag
Flags controlling behavior of code editor.
QFont lexerFont() const
Returns the font to use in the lexer.
QColor lexerColor(QgsCodeEditorColorScheme::ColorRole role) const
Returns the color to use in the lexer for the specified role.
static QColor defaultColor(QgsCodeEditorColorScheme::ColorRole role, const QString &theme=QString())
Returns the default color for the specified role.
static QString stringToPythonLiteral(const QString &string)
Converts a string to a Python string literal.
static bool run(const QString &command, const QString &messageOnError=QString())
Execute a Python statement.
static bool eval(const QString &command, QString &result)
Eval a Python statement.
static bool isValid()
Returns true if the runner has an instance (and thus is able to run commands)
#define QgsDebugError(str)