-
Notifications
You must be signed in to change notification settings - Fork 118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Basic Java Highlighting #38
Open
ghost
wants to merge
1
commit into
Megaxela:master
Choose a base branch
from
unknown repository
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was by mistake, but can probably stay in the PR.