Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ set(INCLUDE_FILES
include/QSyntaxStyle
include/QGLSLCompleter
include/QGLSLHighlighter
include/QJavaHighlighter
include/QLanguage
include/QXMLHighlighter
include/QJSONHighlighter
Expand All @@ -35,6 +36,7 @@ set(INCLUDE_FILES
include/internal/QHighlightBlockRule.hpp
include/internal/QCodeEditor.hpp
include/internal/QCXXHighlighter.hpp
include/internal/QJavaHighlighter.hpp
include/internal/QLineNumberArea.hpp
include/internal/QStyleSyntaxHighlighter.hpp
include/internal/QSyntaxStyle.hpp
Expand All @@ -58,6 +60,7 @@ set(SOURCE_FILES
src/internal/QStyleSyntaxHighlighter.cpp
src/internal/QGLSLCompleter.cpp
src/internal/QGLSLHighlighter.cpp
src/internal/QJavaHighlighter.cpp
src/internal/QLanguage.cpp
src/internal/QXMLHighlighter.cpp
src/internal/QJSONHighlighter.cpp
Expand Down
32 changes: 32 additions & 0 deletions example/resources/code_samples/java.java
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 + "!");
}
}
1 change: 1 addition & 0 deletions example/resources/demo_resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<file>code_samples/cxx.cpp</file>
<file>code_samples/shader.glsl</file>
<file>code_samples/xml.xml</file>
<file>code_samples/java.java</file>
<file>code_samples/json.json</file>
<file>code_samples/lua.lua</file>
<file>code_samples/python.py</file>
Expand Down
3 changes: 3 additions & 0 deletions example/src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QCXXHighlighter>
#include <QGLSLHighlighter>
#include <QXMLHighlighter>
#include <QJavaHighlighter>
#include <QJSONHighlighter>
#include <QLuaHighlighter>
#include <QPythonHighlighter>
Expand Down Expand Up @@ -53,6 +54,7 @@ void MainWindow::initData()
{"C++", loadCode(":/code_samples/cxx.cpp")},
{"GLSL", loadCode(":/code_samples/shader.glsl")},
{"XML", loadCode(":/code_samples/xml.xml")},
{"Java", loadCode(":/code_samples/java.java")},
{"JSON", loadCode(":/code_samples/json.json")},
{"LUA", loadCode(":/code_samples/lua.lua")},
{"Python", loadCode(":/code_samples/python.py")}
Expand All @@ -70,6 +72,7 @@ void MainWindow::initData()
{"C++", new QCXXHighlighter},
{"GLSL", new QGLSLHighlighter},
{"XML", new QXMLHighlighter},
{"Java", new QJavaHighlighter },
{"JSON", new QJSONHighlighter},
{"LUA", new QLuaHighlighter},
{"Python", new QPythonHighlighter},
Expand Down
3 changes: 3 additions & 0 deletions include/QJavaHighlighter
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

#include <internal/QJavaHighlighter.hpp>
42 changes: 42 additions & 0 deletions include/internal/QJavaHighlighter.hpp
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;
};

2 changes: 1 addition & 1 deletion resources/languages/glsl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,4 @@
<name>iimage2DMSArray</name>
<name>uimage2DMSArray</name>
</section>
</root>
</root>
Copy link
Author

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.

62 changes: 62 additions & 0 deletions resources/languages/java.xml
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>
1 change: 1 addition & 0 deletions resources/qcodeeditor_resources.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<file>default_style.xml</file>
<file>languages/glsl.xml</file>
<file>languages/cpp.xml</file>
<file>languages/java.xml</file>
<file>languages/lua.xml</file>
<file>languages/python.xml</file>
</qresource>
Expand Down
114 changes: 114 additions & 0 deletions src/internal/QJavaHighlighter.cpp
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);
}
}