Text editor with syntax highlighting and line numbers?
Asked Answered
H

3

6

This is a bit challenging even probably for a team project, let alone for a one-man implementation, but I was trying to put together a simple yet elegant text editor with syntax highlighting, using a JEditorPane. I stumbled upon this which was discontinued and really hard for me to understand with all the lexer files and .lex stuff inside. I even found in some blog that this project was later taken on by some other team but even yet again discontinued. I don't need it to be too fancy, like having code folding and stuff (even though I am tempted to find out how to do this), but I need at least a basic syntax highlighting to exist and pretty much line numbers on the far left side just like Notepad++ for example. Keep in mind that I only need it to highlight Java source-code, at least for now.

What I am looking for is either a tutorial, a well-documented example and sample code, a pre-made package, even a tool for NetBeans can do the trick, I do not neccesarily need the source code written from scratch, I just need an implementation that can be of use. Thanks in advance!

P.S.This is not gonna be commercial or too big, don't ask why I want to reinvent the wheel when there are so many programming editors out there, I am learning and this came up as a nice exercise for me!

Hoodlum answered 1/10, 2012 at 17:31 Comment(3)
Did you see this tutorial around Netbeans? or did you check this swing component?Bombard
Second link's example does not even compile? Error on line 27: class for org.jdesktop.swingx.JXEditorPane not found!?Hoodlum
JXEditorPane it's part of SwingX components, maybe you don't have the rigth dependency swingx.java.net. For the tutorial maybe you should read the entire series antonioshome.net/kitchen/netbeansBombard
D
6

RSyntaxTextArea is BSD licensed and supports your requirements, plus code folding and more. Very simple to use.

Diannadianne answered 3/10, 2012 at 3:18 Comment(0)
W
1

Well I worked on a similar project and here's what I came up with. As far the line numbers go I used a scrollpane attached to the actual textpane. The scrollpane was then changing numbers with the following code:

public class LineNumberingTextArea extends JTextArea
{
private JTextPane textArea;


/**
 * This is the contructor that creates the LinNumbering TextArea.
 *
 * @param textArea The textArea that we will be modifying to add the 
 * line numbers to it.
 */
public LineNumberingTextArea(JTextPane textArea)
{
    this.textArea = textArea;
    setBackground(Color.BLACK);
    textArea.setFont(new Font("Consolas", Font.BOLD, 14));
    setEditable(false);
}

/**
 * This method will update the line numbers.
 */
public void updateLineNumbers()
{
    String lineNumbersText = getLineNumbersText();
    setText(lineNumbersText);
}


/**
 * This method will set the line numbers to show up on the JTextPane.
 *
 * @return This method will return a String which will be added to the 
 * the lineNumbering area in the JTextPane.
 */
private String getLineNumbersText()
{
    int counter = 0;
    int caretPosition = textArea.getDocument().getLength();
    Element root = textArea.getDocument().getDefaultRootElement();
    StringBuilder lineNumbersTextBuilder = new StringBuilder();
    lineNumbersTextBuilder.append("1").append(System.lineSeparator());

    for (int elementIndex = 2; elementIndex < root.getElementIndex(caretPosition) +2; 
        elementIndex++)
    {
        lineNumbersTextBuilder.append(elementIndex).append(System.lineSeparator());
    }
    return lineNumbersTextBuilder.toString();
}
}

The syntax highlighting is not an easy task, but what I started with was being able to search for strings based off some text files that contained all keywords for a certain language. Basically based off the extension of a file the function would find the correct file and look for words in that file that were contained within the text area.

Womble answered 30/12, 2016 at 2:51 Comment(0)
P
0

I'm write code on C/C++ masm and nasm. On win64. I finded text editor like notepad, with syntax highlighting, line numbers, easy folding code, and be able to set cursor at any position in blank (empty) line. I try : notepad++ notepad4 gvim WebStorm sublime_text EmEditor UltraEdit VS code and many other text editors.

So, only Visual Studio had these.
for ASM  I instal ASMdude (syntax highlighting).
folding code - right click mouse -  ....  - fold and unfold.
ALT+ Left mouse - in any place of string - and you can write code.

Just create blank project,  and add  file/files   you need.
----------------------------------------------------------------
Pairoar answered 15/9 at 15:24 Comment(1)
What did you not find in these editors? All of these have features you described. You'll need to describe the advantages and disadvantages of each. If you really want to answer to a 12-year-old question.Dictograph

© 2022 - 2024 — McMap. All rights reserved.