bobbylight / RSyntaxTextArea

A syntax highlighting, code folding text editor for Java Swing applications.
BSD 3-Clause "New" or "Revised" License
1.12k stars 259 forks source link

[Bug]: C/C++: Highlight the import files #461

Closed lzhiyong closed 2 years ago

lzhiyong commented 2 years ago

Description the CPlusPlusTokenMaker.flex and CTokenMaker.flex can't parse header files correctly.

like this:

IMG_20220929_190938

Expected behavior Can correctly highlight header files.

like this:

IMG_20220929_191008

Java version openjdk-17

Additional context I can't fix this as I'm not familiar with the Jflex syntax. see the cpp.flex

Pseudo code:

/* Add state to import files */
%state IMPORT_FILE

%%

<YYINITIAL> {
    ...
    /* Preprocessor directives */
    "#"{WhiteSpace}*{PreprocessorWord}    { start = zzMarkedPos-2; yybegin(IMPORT_FILE); }

    <IMPORT_FILE>{
        /* <xxx.h> or <xxx> as the StringLiteral  */
    {WhiteSpace}\*<{AnyChrChr}>    { int temp=zzStartRead; addToken(start,zzStartRead-1, Token.PREPROCESSOR); addToken(temp,zzMarkedPos-1, Token.LITERAL_CHAR); start = zzMarkedPos; }
    \n          { addToken(start,zzStartRead-1, Token.LITERAL_CHAR); return firstToken; }
    <<EOF>>         { addToken(start,zzStartRead-1, Token.LITERAL_CHAR); return firstToken; }
    }

}
lzhiyong commented 2 years ago

@bobbylight thanks

lzhiyong commented 2 years ago

IMG_20221026_172140


@bobbylight, there are still some problems.

// contains /
#include <sys/types.h>

// contains -
#include <gnu/libc-version.h>

// contains multiple spaces between # and include
// contains multiple spaces between include and <
# include    <stdio.h>
#        include <math.h>

I think the correct expression should be "#"[ \t\f]*"include"[ \t\f]*"<"[A-Za-z0-9_.-/]+">"


In addition, is there a way to highlight the import file? like Java, kotlin...

// highlight the java.util.ArrayList
import java.util.ArrayList;
bobbylight commented 2 years ago

Thanks, I've addressed the whitespace and / and - characters and published a 3.3.1-SNAPSHOT build to Sonatype with these changes.

As for your second question, are you asking for a way to highlight the imported file a different color than the surrounding < and > characters? Or are you asking if we can be smarter and highlight files known to be in the standard library?

lzhiyong commented 2 years ago

As for your second question, are you asking for a way to highlight the imported file a different color than the surrounding < and > characters? Or are you asking if we can be smarter and highlight files known to be in the standard library?

@bobbylight thanks, Specify package name color for example:

// like include 

// highlight the 'com.xxx.package_name'
package com.xxx.package_name;

// highlight the 'com.xxx.class_name'
import com.xxx.class_name;