Skip to content

Add regex for line comparison#243

Open
cns-solutions-admin wants to merge 5 commits intomathieucarbou:masterfrom
CNS-Solutions:master
Open

Add regex for line comparison#243
cns-solutions-admin wants to merge 5 commits intomathieucarbou:masterfrom
CNS-Solutions:master

Conversation

@cns-solutions-admin
Copy link

This solves #242 by adding a new optional regex to compare the lines other than the first for multiline headers.

@cns-solutions-admin
Copy link
Author

Also converts a inline header style's name to lower case, otherwise the comparison (and the example in the docs) fails.

* In maven/xml we need xml:space="preserve" to prevent trimming of text values.
* The XML attribute is multiline, not isMultiline.
* inline styles are only available in version 4.2 - we should note this, as the suggested version is 4.1.
* the inline example does only work, if the inline style name is lowercase. code fixed in previous PR.
@mathieucarbou
Copy link
Owner

Hello,
Why changing the header definition didn't work for your use case ?

@mathieucarbou mathieucarbou self-requested a review November 26, 2021 12:29
@mathieucarbou mathieucarbou added in:core MLP core module is:enhancement Enhancement to an existing feature todo Accepted items from the backlog which can be worked on labels Nov 26, 2021
@cns-solutions-admin
Copy link
Author

Changing the header definition did not work, because the detection of header lines was orginally done by comparing lines with startsWith(beforeEachLine.rtrim())
the old " * something" does not start with "**".

By introducing a (optional) RegEx for the header lines, this and most other use cases can be covered.
It also fits with the current configuration, where we already have RegExs for start and end line.

Copy link
Owner

@mathieucarbou mathieucarbou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also squash and rebase. Thanks 👍

docs/index.md Outdated
<lastLineDetectionPattern>.*\*/(\s|\t)*$</lastLineDetectionPattern>
<allowBlankLines>false</allowBlankLines>
<isMultiline>true</isMultiline>
<multiline>true</multiline>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove any change regarding multiline: I've correctly fixed in #247


private Pattern skipLinePattern;
private Pattern firstLineDetectionPattern;
private Pattern otherLineDetectionPattern;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer that the name is called middleLineDetectionPattern

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except it is for all lines except the first line - the last line must also pass this pattern (or the beforeEachLine test).
Finding the best name is often the most difficult task ;-)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the last line must also pass this pattern

Why ? There is a pattern for the last line.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the code still checks beforeEachLine. I didn't want to change the logic and the RegEx happens when the startsWith(beforeEachLine) check happens.

check("endLine", this.endLine);
check("afterEachLine", this.afterEachLine);
check("firstLineDetectionPattern", this.firstLineDetectionPattern);
// other line detection pattern can be null
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for this comment: it's like beforeEachLine ;-)

final HeaderDefinition headerDefinition = new HeaderDefinition(
"javadoc2_style", "/**", " ** ", " **/", "", null, "^\\s*/\\*.*$", "^\\s*\\*.*$", "^.*\\*/\\s*$", false, true, false);
return new HeaderParser(new FileContent(new File(fileName), System.getProperty("file.encoding")), headerDefinition, new String[]{"copyright"});
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would need also to have a more complete test (like CompleteMojoTest does) to also test the other plugin actions, like really testing the issue you want to fix.

this.afterEachLine = afterEachLine;
this.skipLinePattern = compile(skipLinePattern);
this.firstLineDetectionPattern = compile(firstLineDetectionPattern);
this.otherLineDetectionPattern = compile(otherLineDetectionPattern);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you would need to update setPropertyFromString(...) too for xml header definition.

@mathieucarbou mathieucarbou modified the milestones: 4.3, 4.2 Nov 26, 2021
@mathieucarbou mathieucarbou added is:bug Bugs to fix and removed is:enhancement Enhancement to an existing feature labels Nov 26, 2021
@mathieucarbou mathieucarbou modified the milestones: 4.2, 4.3 Mar 23, 2023
@hazendaz hazendaz modified the milestones: 4.4, 4.5 Jan 20, 2024
@mathieucarbou mathieucarbou removed this from the 4.5 milestone May 11, 2024
@mathieucarbou mathieucarbou requested a review from Copilot July 8, 2025 08:03
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds support for a customizable regex to detect header lines beyond the first in multi-line comments, along with sample files, tests, API changes, and documentation updates.

  • Introduced otherLineDetectionPattern in HeaderDefinition and HeaderStyle
  • Switched HeaderParser to use isOtherHeaderLine(...) instead of a fixed prefix check
  • Added three Java header fixtures and corresponding parser tests, plus whitespace‐preservation updates in docs

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
license-maven-plugin/src/test/resources/doc/java1.txt New sample header with two‐star prefix
license-maven-plugin/src/test/resources/doc/java2.txt New sample header with single‐star prefix
license-maven-plugin/src/test/resources/doc/java3.txt New sample header without space after slash
license-maven-plugin/src/test/java/com/mycila/maven/plugin/license/header/HeaderParserTest.java Added tests for the three Java header samples
license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderParser.java Replaced hardcoded startsWith(before) with isOtherHeaderLine
license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java Added otherLineDetectionPattern, new constructor, and isOtherHeaderLine
license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/HeaderStyle.java Exposed otherLineDetectionPattern as a plugin parameter
docs/index.md Added xml:space="preserve", marked version, but docs lack new pattern
Comments suppressed due to low confidence (2)

docs/index.md:545

  • The <javadoc_style> snippet doesn’t include the new <otherLineDetectionPattern> element. Please add an example of this setting to document the new optional regex.
    <firstLineDetectionPattern>(\s|\t)*/\*.*$</firstLineDetectionPattern>

license-maven-plugin/src/main/java/com/mycila/maven/plugin/license/header/HeaderDefinition.java:67

  • The variable isMultiline is not defined in this constructor scope; it should be multiLine to match the parameter name.
     this(type, firstLine, beforeEachLine, endLine, afterEachLine, skipLinePattern, firstLineDetectionPattern, null, lastLineDetectionPattern, allowBlankLines, isMultiline, padLines);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in:core MLP core module is:bug Bugs to fix todo Accepted items from the backlog which can be worked on

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Header is added instead of being replaced, if formats differ

4 participants