-1

Following is my Java code

import java.io.*;

public class UnderLine {
    public static void main(String[] args) throws Exception {
        String thisLine = null;
        String Test = "Hello";

        try {
            // open input stream test.txt for reading purpose.
            BufferedReader br = new BufferedReader(new FileReader("F:\\Java\\Testprojects\\UnderLine.java"));
            while ((thisLine = br.readLine()) != null) {
                if(thisLine=="        String Test = \"Hello\";\n") {    
                    // underLine the line which has String test
                }
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

In the above code I read the same source code by giving its path. I want to underline (the red line coming under the words as if a normal code error occurs) the code line with

String Test = "Hello";.

How can I do it?

Robert
  • 7,394
  • 40
  • 45
  • 64
Kasun Kumara
  • 11
  • 1
  • 7

2 Answers2

1

What you are asking (underlining in an actual Java source code file) is not possible. Any form of added markup in the source code would lead to Java syntax errors when you compiled the file.

If you actually want to view the source code in your Eclipse IDE with underlines and other markup added by Eclipse, then you will need to develop an Eclipse plugin to do this. The plugin would make of the relevant Eclipse APIs to add the highlighting in the view while the file is open in the IDE. The "source" for the markup would need to be saved separately from the Java source file.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Then how come plugins such as Sonarlint underline strings with quality issues but can be compiled without any errors? – Kasun Kumara May 20 '18 at 14:37
  • 1
    Because they are showing you a **view** of the source with underlines added. The underlines are not being added to the source files themselves by SonarLint. – Stephen C May 20 '18 at 22:36
  • yeah I need to add a view like that! Is it possible? – Kasun Kumara May 21 '18 at 02:18
  • Yea, it is possible if you write your own Eclipse plugin. I'm not aware of an existing plugin that does this though. Lots of plugins *use* underlining, but AFAIK none allow you to apply styling by hand. (The Eclipse API functionality you should look for is StyledText ... apparently.) – Stephen C May 21 '18 at 02:53
0

As the commenters already said, any kind of formatting is not possible in java source files as they are plain text files.

Since you use IntelliJ, you can simply hit F11 on your keyboard which will create a bookmark in the current line. You will see a white line appear in the scrollbar which allows you to return to that line of code easily. To see all bookmarks that you placed in your entire project, hit Shift+F11. You can find more information on that topic in the IntelliJ help section.

vatbub
  • 2,713
  • 18
  • 41
  • Can't I add a view using openapi and SyntaxHighlighter class? – Kasun Kumara May 21 '18 at 02:17
  • What do you mean by that? Also, consider why you do that in the first place. Why do you wish to highlight the line? If it's for explanatory reasons, place a comment above the line. If you wish to find the line easier, place a Bookmark using F11. Don't spend too much time on the highliting as it will not be of any benefit for the user. – vatbub May 21 '18 at 19:17