INRIA / INRIA/spoon

[Bug]: Header comments are removed

Open
#5,085 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

### Describe the bug

The `//` comments are removed by default. This violates the principle of least surprise, IMO.

### Source code you are trying to analyze/transform

```
cd /tmp
git clone https://github.com/DaveJarvis/nts
```

### Source code for your Spoon processing

```Java
import spoon.Launcher;
import spoon.refactoring.Refactoring;
import spoon.support.sniper.SniperJavaPrettyPrinter;

import java.io.File;
import java.util.regex.Pattern;

public class App {
public static void main( final String[] args ) {
final var outputDir = new File("/tmp/nts-new");
final var spoon = new Launcher();

final var env = spoon.getEnvironment();
env.setPrettyPrinterCreator(
() -> new SniperJavaPrettyPrinter( env )
);
env.setAutoImports( true );

spoon.setSourceOutputDirectory( outputDir );

spoon.addInputResource( "/tmp/nts/app/src/main/java/nts/app" );
spoon.addInputResource( "/tmp/nts/dvi/src/main/java/nts/dvi" );
spoon.addInputResource( "/tmp/nts/png/src/main/java/nts/png" );
spoon.addInputResource( "/tmp/nts/svg/src/main/java/nts/svg" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/align" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/base" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/builder" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/command" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/hyph" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/io" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/math" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/noad" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/node" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/render" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/tex" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/tfm" );
spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/typo" );
spoon.addInputResource( "/tmp/nts/tex/src/test/java/nts/io" );

final var model = spoon.buildModel();
final var p = Pattern.compile( "^(?!Kt).*" );

for( final var clazz : model.getAllTypes() ) {
final var name = clazz.getSimpleName();
final var m = p.matcher( name );

if( m.matches() ) {
final var newName = "Kt" + name;

System.out.println( "rename " + name + " to " + newName );
Refactoring.changeTypeName( clazz, newName );
}
}

if( outputDir.exists() || outputDir.mkdirs() ) {
spoon.prettyprint();
}
}
}
```

### Actual output

```Java
package nts.align;public class KtEndv extends KtBuilderCommand {

public final void addOn(KtLog log) {
log.add("end of alignment template");
}
}
```

### Expected output

```Java
// Copyright 2001 by
// DANTE e.V. and any individual authors listed elsewhere in this file.
//
// This file is part of the NTS system.
// ------------------------------------
//
// It may be distributed and/or modified under the
// conditions of the NTS Public License (NTSPL), either version 1.0
// of this license or (at your option) any later version.
// The latest version of this license is in
// http://www.dante.de/projects/nts/ntspl.txt
// and version 1.0 or later is part of all distributions of NTS
// version 1.0-beta or later.
//
// The list of all files belonging to the NTS distribution is given in
// the file `manifest.txt'.
//
// Filename: nts/align/Endv.java
// $Id: Endv.java,v 1.1.1.1 2001/03/12 21:42:18 ksk Exp $
package nts.align;

import nts.io.KtLog;
import nts.typo.KtBuilderCommand;

public class KtEndv extends KtBuilderCommand {

public final void addOn(KtLog log) {
log.add("end of alignment template");
}
}
```

### Spoon Version

Latest

### JVM Version

18

### What operating system are you using?

Linux

Contributor guide

Open the contributing guide

Research direction

Start with the SniperJavaPrettyPrinter entry point shown in the reproduction and run the supplied App against the NTS sources. Trace how header `//` comments are handled during pretty-printing, then verify that the generated Endv.java retains the original comments and formatting shown in the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.