forked from Megaxela/QCodeEditor
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Java highlighting from Megaxela#38
- Loading branch information
Showing
10 changed files
with
262 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import java.util.Scanner; | ||
|
||
/* This program is simply for demonstrating | ||
* the highlighting of Java syntax. | ||
*/ | ||
|
||
class Animal { } | ||
|
||
class Dog extends Animal { } | ||
|
||
public class Example { | ||
/** Prompts the user for a string and returns what they entered. | ||
* @param message The message to display to the user. | ||
* @return The string that the user entered. | ||
* */ | ||
static String prompt(String message) { | ||
|
||
Scanner scanner = new Scanner(System.in); | ||
|
||
System.out.printf("%s: ", message); | ||
|
||
return scanner.nextLine(); | ||
} | ||
public static void main(String[] args) { | ||
|
||
double pi = 3.1415; | ||
|
||
String name = prompt("Enter your name"); | ||
|
||
System.out.println("Nice to meet you, " + name + "!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
#include <internal/QJavaHighlighter.hpp> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#pragma once | ||
|
||
// QCodeEditor | ||
#include <QStyleSyntaxHighlighter> // Required for inheritance | ||
#include <QHighlightRule> | ||
|
||
// Qt | ||
#include <QRegularExpression> | ||
#include <QVector> | ||
|
||
class QSyntaxStyle; | ||
|
||
/** | ||
* @brief Derived to implement highlighting of Java code. | ||
*/ | ||
class QJavaHighlighter : public QStyleSyntaxHighlighter | ||
{ | ||
Q_OBJECT | ||
public: | ||
|
||
/** | ||
* @brief Constructs a new instance of a Java highlighter. | ||
* @param document The text document to be highlighted. | ||
* This may be a null pointer. | ||
*/ | ||
explicit QJavaHighlighter(QTextDocument* document=nullptr); | ||
|
||
protected: | ||
/** | ||
* @brief Derived to highlight blocks of Java code. | ||
* @param text The block of text containing Java code. | ||
*/ | ||
void highlightBlock(const QString& text) override; | ||
|
||
private: | ||
|
||
QVector<QHighlightRule> m_highlightRules; | ||
|
||
QRegularExpression m_commentStartPattern; | ||
QRegularExpression m_commentEndPattern; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -351,4 +351,4 @@ | |
<name>iimage2DMSArray</name> | ||
<name>uimage2DMSArray</name> | ||
</section> | ||
</root> | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<root> | ||
<section name="Keyword"> | ||
<name>abstract</name> | ||
<name>assert</name> | ||
<name>break</name> | ||
<name>case</name> | ||
<name>catch</name> | ||
<name>const</name> | ||
<name>continue</name> | ||
<name>default</name> | ||
<name>do</name> | ||
<name>else</name> | ||
<name>extends</name> | ||
<name>final</name> | ||
<name>finally</name> | ||
<name>for</name> | ||
<name>if</name> | ||
<name>goto</name> | ||
<name>implements</name> | ||
<name>import</name> | ||
<name>instanceof</name> | ||
<name>native</name> | ||
<name>new</name> | ||
<name>package</name> | ||
<name>private</name> | ||
<name>protected</name> | ||
<name>public</name> | ||
<name>return</name> | ||
<name>static</name> | ||
<name>strictfp</name> | ||
<name>super</name> | ||
<name>switch</name> | ||
<name>synchronized</name> | ||
<name>this</name> | ||
<name>throw</name> | ||
<name>throws</name> | ||
<name>transient</name> | ||
<name>try</name> | ||
<name>volatile</name> | ||
<name>while</name> | ||
|
||
<name>true</name> | ||
<name>false</name> | ||
<name>null</name> | ||
|
||
</section> | ||
<section name="PrimitiveType"> | ||
<name>boolean</name> | ||
<name>byte</name> | ||
<name>char</name> | ||
<name>class</name> | ||
<name>double</name> | ||
<name>enum</name> | ||
<name>float</name> | ||
<name>int</name> | ||
<name>interface</name> | ||
<name>long</name> | ||
<name>short</name> | ||
<name>void</name> | ||
</section> | ||
</root> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// QCodeEditor | ||
#include <QJavaHighlighter> | ||
#include <QSyntaxStyle> | ||
#include <QLanguage> | ||
|
||
// Qt | ||
#include <QFile> | ||
|
||
|
||
QJavaHighlighter::QJavaHighlighter(QTextDocument* document) : | ||
QStyleSyntaxHighlighter(document), | ||
m_highlightRules (), | ||
m_commentStartPattern(QRegularExpression(R"(/\*)")), | ||
m_commentEndPattern (QRegularExpression(R"(\*/)")) | ||
{ | ||
Q_INIT_RESOURCE(qcodeeditor_resources); | ||
|
||
QFile fl(":/languages/java.xml"); | ||
|
||
if (!fl.open(QIODevice::ReadOnly)) | ||
{ | ||
return; | ||
} | ||
|
||
QLanguage language(&fl); | ||
|
||
if (!language.isLoaded()) | ||
{ | ||
return; | ||
} | ||
|
||
auto keys = language.keys(); | ||
for (auto&& key : keys) | ||
{ | ||
auto names = language.names(key); | ||
for (auto&& name : names) | ||
{ | ||
m_highlightRules.append({ | ||
QRegularExpression(QString(R"(\b%1\b)").arg(name)), | ||
key | ||
}); | ||
} | ||
} | ||
|
||
// Numbers | ||
m_highlightRules.append({ | ||
QRegularExpression(R"((?<=\b|\s|^)(?i)(?:(?:(?:(?:(?:\d+(?:'\d+)*)?\.(?:\d+(?:'\d+)*)(?:e[+-]?(?:\d+(?:'\d+)*))?)|(?:(?:\d+(?:'\d+)*)\.(?:e[+-]?(?:\d+(?:'\d+)*))?)|(?:(?:\d+(?:'\d+)*)(?:e[+-]?(?:\d+(?:'\d+)*)))|(?:0x(?:[0-9a-f]+(?:'[0-9a-f]+)*)?\.(?:[0-9a-f]+(?:'[0-9a-f]+)*)(?:p[+-]?(?:\d+(?:'\d+)*)))|(?:0x(?:[0-9a-f]+(?:'[0-9a-f]+)*)\.?(?:p[+-]?(?:\d+(?:'\d+)*))))[lf]?)|(?:(?:(?:[1-9]\d*(?:'\d+)*)|(?:0[0-7]*(?:'[0-7]+)*)|(?:0x[0-9a-f]+(?:'[0-9a-f]+)*)|(?:0b[01]+(?:'[01]+)*))(?:u?l{0,2}|l{0,2}u?)))(?=\b|\s|$))"), | ||
"Number" | ||
}); | ||
|
||
// Strings | ||
m_highlightRules.append({ | ||
QRegularExpression(R"("[^\n"]*")"), | ||
"String" | ||
}); | ||
|
||
// Single line | ||
m_highlightRules.append({ | ||
QRegularExpression(R"(//[^\n]*)"), | ||
"Comment" | ||
}); | ||
} | ||
|
||
void QJavaHighlighter::highlightBlock(const QString& text) | ||
{ | ||
for (auto& rule : m_highlightRules) | ||
{ | ||
auto matchIterator = rule.pattern.globalMatch(text); | ||
|
||
while (matchIterator.hasNext()) | ||
{ | ||
auto match = matchIterator.next(); | ||
|
||
setFormat( | ||
match.capturedStart(), | ||
match.capturedLength(), | ||
syntaxStyle()->getFormat(rule.formatName) | ||
); | ||
} | ||
} | ||
|
||
setCurrentBlockState(0); | ||
|
||
int startIndex = 0; | ||
if (previousBlockState() != 1) | ||
{ | ||
startIndex = text.indexOf(m_commentStartPattern); | ||
} | ||
|
||
while (startIndex >= 0) | ||
{ | ||
auto match = m_commentEndPattern.match(text, startIndex); | ||
|
||
int endIndex = match.capturedStart(); | ||
int commentLength = 0; | ||
|
||
if (endIndex == -1) | ||
{ | ||
setCurrentBlockState(1); | ||
commentLength = text.length() - startIndex; | ||
} | ||
else | ||
{ | ||
commentLength = endIndex - startIndex + match.capturedLength(); | ||
} | ||
|
||
setFormat( | ||
startIndex, | ||
commentLength, | ||
syntaxStyle()->getFormat("Comment") | ||
); | ||
startIndex = text.indexOf(m_commentStartPattern, startIndex + commentLength); | ||
} | ||
} |