raydac / java-comment-preprocessor

preprocessor for computer languages with C-like comment format (C/C++/Java/Go)
Apache License 2.0
172 stars 27 forks source link

Remove only comments with macros? #46

Closed Lilianne-Blaze closed 9 months ago

Lilianne-Blaze commented 9 months ago

Could you please add an option to Maven plugin that allows removing comments but only those that contain JCP macros? So that javadocs and code-explaining comments remain untouched?

raydac commented 9 months ago

if file preprocessed then such comments will be removed automatically and how to be if file not preprocessed but for instance such code snippet cleared of jcp comments?

//#if DEBUG
  // Hello debug
  System.out.println("Hello debug");
//#else
  // No debug
  System.out.println("No debug");
//#endif

we will get clean result


  // Hello debug
  System.out.println("Hello debug");

  // No debug
  System.out.println("No debug");

how to be in the case?

Lilianne-Blaze commented 9 months ago

Basically given that code:

    public static long getMyPid() {
        System.out.println("(getMyPid called)");
        long retVal = 0;
        // #if preprocess.java.version >= 9
        // use new API
        retVal = ProcessHandle.current().pid();
        // #else
        // use old MXBean hack
        // $retVal = getMyPidOld();
        // #endif
        return retVal;
    }

Normally I get this:

    public static long getMyPid() {
        System.out.println("(getMyPid called)");
        long retVal = 0;
        // JCP! if preprocess.java.version >= 9
        // JCP> // use new API
        // JCP> retVal = ProcessHandle.current().pid();
        // JCP! else
        // use old MXBean hack
        retVal = getMyPidOld();
        // JCP! endif
        return retVal;
    }

With keepComments=false I get this:

    public static long getMyPid() {
        System.out.println("(getMyPid called)");
        long retVal = 0;

        retVal = getMyPidOld();

        return retVal;
    }

I would like to get this:

    public static long getMyPid() {
        System.out.println("(getMyPid called)");
        long retVal = 0;

        // use old MXBean hack
        retVal = getMyPidOld();

        return retVal;
    }

Something like keepNormalComments or keepNonmacroComments would be nice

raydac commented 9 months ago

ok, I will check

raydac commented 9 months ago

I've made some improvements and now keepComments allows such variants: