drccrd commented on code in PR #4083:
URL:
https://github.com/apache/incubator-kie-tools/pull/4083#discussion_r4662821815
##########
packages/drools-lsp/drools-completion/src/main/java/org/drools/completion/DRLLintHelper.java:
##########
@@ -756,50 +845,32 @@ private static List<Diagnostic>
lintMissingRuleEnds(String sanitized,
public static List<Diagnostic> lintUnknownTypes(String text, Path
documentPath,
Map<Path, String>
openFiles, ClassIndex classIndex,
ClassMemberIndex
memberIndex, boolean classpathResolved) {
+ return lintUnknownTypes(text, documentPath, openFiles, classIndex,
memberIndex, classpathResolved, DRLLintSuppression.parse(text));
+ }
+
+ public static List<Diagnostic> lintUnknownTypes(String text, Path
documentPath,
+ Map<Path, String>
openFiles, ClassIndex classIndex,
+ ClassMemberIndex
memberIndex, boolean classpathResolved,
+
DRLLintSuppression.SuppressionIndex suppression) {
if (text == null || text.isEmpty()) {
return Collections.emptyList();
}
return lintUnknownTypes(text,
DRLParsers.silent(text).compilationUnit(), documentPath,
- openFiles, classIndex, memberIndex, classpathResolved);
+ openFiles, classIndex, memberIndex, classpathResolved,
suppression);
}
- /**
- * Flags type references that resolve to no known type and, for likely
typos
- * (within bounded Levenshtein ≤ 2 of a known name), suggests the right
one.
- * Candidates come from three sources:
- * <ul>
- * <li>pattern object types, read from the parse tree (so nested
- * {@code exists}/{@code not}/{@code from}/{@code accumulate}
patterns
- * are covered);</li>
- * <li>upper-case-led qualified references in LHS constraints, e.g.
- * {@code Pet.Dog.name} — the head type is
- * resolved, then the first upper-case member segment is verified
- * against the head's reflected members (enum constants, fields,
nested
- * types); lower-case property segments are not chased;</li>
- * <li>{@code new T(...)} in the consequence, matched by regex — the RHS
is
- * lexed into opaque chunks the grammar does not structure.</li>
- * </ul>
- * A type is known when it is a declared type or resolves via
- * {@link DRLCompletionHelper#resolveFqcn} (imports, wildcard imports, the
- * class index, or {@code java.lang}) — the same resolution hover,
completion
- * and go-to-definition use. The caller supplies the parsed {@code cu}
- * (shared with the syntax pass) and it is reused here for declared-type
- * extraction. Self-gated by {@code drools.lsp.lint.unknownTypes}
- * (default {@code warning}; {@code off} disables).
- *
- * @param cu the parsed compilation unit (shared with the
syntax pass); may be {@code null}
- * @param documentPath document path for sibling declared-type
lookup; may be {@code null}
- * @param openFiles open unsaved sibling buffers, so cross-file
declares count; may be empty
- * @param classIndex resolved classpath index, for type resolution
and suggestions
- * @param memberIndex member reflection, for verifying classpath
qualified-reference members
- * @param classpathResolved whether the project classpath has resolved;
when {@code false}, only
- * checks derivable from the DRL alone run
(declared types and declared-enum
- * members), since a non-declared name can't be
confirmed unknown without it
- */
public static List<Diagnostic> lintUnknownTypes(String text,
DRL10Parser.CompilationUnitContext cu,
Review Comment:
It seems the javadoc description of this function was removed, but I don't
see any reason for this.
##########
packages/drools-lsp/drools-completion/src/main/java/org/drools/completion/DRLLintHelper.java:
##########
@@ -101,17 +104,85 @@ public final class DRLLintHelper {
Pattern.compile("(?<![\\w.$])([A-Z][A-Za-z0-9_$]*(?:\\.[A-Za-z0-9_$]+)+)");
private static final Pattern RULE_KEYWORD =
- Pattern.compile("^\\s*(rule|query)\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^\\s*(rule|query)\\b");
private static final Pattern DECLARE_KEYWORD =
- Pattern.compile("^\\s*declare\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^\\s*declare\\b");
private static final Pattern END_AT_START =
- Pattern.compile("^end\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^end\\b");
private static final Pattern END_AT_END =
- Pattern.compile("(?:^|\\s)end$", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("(?:^|\\s)end$");
private static final Pattern WHEN_KEYWORD =
- Pattern.compile("^\\s*when\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^\\s*when\\b");
private static final Pattern THEN_KEYWORD =
- Pattern.compile("^\\s*then\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^\\s*then\\b");
+
+ /** Represents a scanned rule or query block and whether it was properly
closed. */
+ public static final class RuleBlock {
+ public final int startLine;
+ public final int endLine; // -1 if unclosed
+ public final int startCol;
+ public final boolean isQuery;
+
+ public RuleBlock(int startLine, int endLine, int startCol, boolean
isQuery) {
+ this.startLine = startLine;
+ this.endLine = endLine;
+ this.startCol = startCol;
+ this.isQuery = isQuery;
+ }
+
+ public boolean isClosed() {
+ return endLine >= 0;
+ }
+ }
+
+ /**
+ * Scans {@code sanitized} text for rule and query blocks, tracking
unclosed rules
+ * with a stack so unclosed rules do not prematurely swallow subsequent
blocks.
+ */
+ public static List<RuleBlock> scanRuleBlocks(String sanitized) {
+ String[] lines = sanitized.split("\r?\n", -1);
+ List<RuleBlock> completed = new ArrayList<>();
+ List<RuleBlock> openRules = new ArrayList<>();
+ boolean inDeclare = false;
+
+ for (int i = 0; i < lines.length; i++) {
+ String raw = lines[i];
+ String trimmed = raw.trim();
+ if (trimmed.isEmpty()) {
+ continue;
+ }
+
+ Matcher rm = RULE_KEYWORD.matcher(trimmed);
+ if (rm.find()) {
+ inDeclare = false;
Review Comment:
the special handling for `declare` was moved here from 'lintMissingRuleEnds'
but the corresponding javadoc comment was not
##########
packages/drools-lsp/drools-completion/src/main/java/org/drools/completion/DRLLintHelper.java:
##########
Review Comment:
the mvel pass still caps inside `findLhsAccessorFindings`, before the
suppression check, so 20 suppressed getters hide the next real one (warning and
quick fix).
A centralized helper for the suppression + count logic would help prevent
drift on this point, but is not necessary.
##########
packages/drools-lsp/drools-lsp-server/src/main/java/org/drools/lsp/server/DroolsLspDocumentService.java:
##########
@@ -757,7 +761,8 @@ static List<Either<Command, CodeAction>>
buildPropertyAccessActions(
String uri, String text, Range requested) {
List<TextEdit> edits;
try {
- edits = DRLLintHelper.mvelPropertyAccessEdits(text);
+ DRLLintSuppression.SuppressionIndex suppression =
DRLLintSuppression.parse(text);
Review Comment:
this re-parses on every code action, could it not use the index built in
`validate()`?
##########
packages/drools-lsp/drools-completion/src/main/java/org/drools/completion/DRLLintHelper.java:
##########
@@ -390,18 +482,29 @@ private static List<Diagnostic>
lintMvelPropertyAccess(String sanitized,
}
/**
- * Returns the {@link TextEdit}s that rewrite every flagged LHS getter call
+ * Returns the {@link TextEdit}s that rewrite every unsuppressed flagged
LHS getter call
* to MVEL property access ({@code getCode()} → {@code code}). Drives the
- * code-action quick-fix; uses the same detection as
- * {@link #lintMvelPropertyAccess}.
+ * code-action quick-fix.
*/
public static List<TextEdit> mvelPropertyAccessEdits(String text) {
+ return mvelPropertyAccessEdits(text, null);
Review Comment:
this passes `null`, so it doesn't skip suppressed getters like the javadoc
says. `lint(text)` parses the directives, could this do the same?
##########
packages/drools-lsp/drools-completion/src/main/java/org/drools/completion/DRLLintHelper.java:
##########
@@ -101,17 +104,85 @@ public final class DRLLintHelper {
Pattern.compile("(?<![\\w.$])([A-Z][A-Za-z0-9_$]*(?:\\.[A-Za-z0-9_$]+)+)");
private static final Pattern RULE_KEYWORD =
- Pattern.compile("^\\s*(rule|query)\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^\\s*(rule|query)\\b");
private static final Pattern DECLARE_KEYWORD =
- Pattern.compile("^\\s*declare\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^\\s*declare\\b");
private static final Pattern END_AT_START =
- Pattern.compile("^end\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^end\\b");
private static final Pattern END_AT_END =
- Pattern.compile("(?:^|\\s)end$", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("(?:^|\\s)end$");
private static final Pattern WHEN_KEYWORD =
- Pattern.compile("^\\s*when\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^\\s*when\\b");
private static final Pattern THEN_KEYWORD =
- Pattern.compile("^\\s*then\\b", Pattern.CASE_INSENSITIVE);
+ Pattern.compile("^\\s*then\\b");
+
+ /** Represents a scanned rule or query block and whether it was properly
closed. */
+ public static final class RuleBlock {
+ public final int startLine;
+ public final int endLine; // -1 if unclosed
+ public final int startCol;
+ public final boolean isQuery;
+
+ public RuleBlock(int startLine, int endLine, int startCol, boolean
isQuery) {
+ this.startLine = startLine;
+ this.endLine = endLine;
+ this.startCol = startCol;
+ this.isQuery = isQuery;
+ }
+
+ public boolean isClosed() {
+ return endLine >= 0;
+ }
+ }
+
+ /**
+ * Scans {@code sanitized} text for rule and query blocks, tracking
unclosed rules
+ * with a stack so unclosed rules do not prematurely swallow subsequent
blocks.
+ */
+ public static List<RuleBlock> scanRuleBlocks(String sanitized) {
Review Comment:
it looks like `scanRuleBlocks` and `RuleBlock` above could be private. They
are also inserted before the last two field constants, which looks weird,
rather than after them
##########
packages/drools-lsp/drools-completion/src/main/java/org/drools/completion/DRLLintSuppression.java:
##########
@@ -0,0 +1,570 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.drools.completion;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.Token;
+import org.drools.drl.parser.antlr4.DRL10Lexer;
+import org.drools.drl.parser.antlr4.DRL10ParserHelper;
+import org.eclipse.lsp4j.Diagnostic;
+import org.eclipse.lsp4j.DiagnosticSeverity;
+import org.eclipse.lsp4j.Position;
+import org.eclipse.lsp4j.Range;
+
+/**
+ * Parses in-source linter suppression directives under the {@code
@drl-lsp-lint}
+ * namespace from ANTLR comment tokens and filters out suppressed diagnostics.
+ *
+ * <p>Supported directives:
+ * <ul>
+ * <li>{@code @drl-lsp-lint:disable-file [code, ...]} — suppresses
matching codes (or all codes) for the entire file</li>
+ * <li>{@code @drl-lsp-lint:disable-line [code, ...]} — suppresses
matching codes on the current line</li>
+ * <li>{@code @drl-lsp-lint:disable-next-line [code, ...]} —
suppresses matching codes on the line immediately following (for block
comments, the line following closing {@code */})</li>
+ * <li>{@code @drl-lsp-lint:disable-rule [code, ...]} — suppresses
matching codes for the entire enclosing or following rule/query</li>
+ * <li>{@code @drl-lsp-lint:disable [code, ...]} — starts a
suppression region from this line downwards</li>
+ * <li>{@code @drl-lsp-lint:enable [code, ...]} — ends a suppression
region from this line downwards</li>
+ * </ul>
+ *
+ * <p>Directives apply strictly to {@code drools-lint}, {@code drools-type},
and
+ * {@code drools-deprecated} diagnostics. Parser/syntax errors ({@code
drools-drl-parser})
+ * and compiler diagnostics ({@code drools}) are never suppressed.
+ */
+public final class DRLLintSuppression {
+
+ private static final Pattern DIRECTIVE_PATTERN =
Pattern.compile("(?i)@drl-lsp-lint:([a-zA-Z0-9_-]+)");
+
+ public static final Set<String> VALID_ACTIONS = Set.of(
+ "disable-file",
+ "disable-next-line",
+ "disable-line",
+ "disable-rule",
+ "disable",
+ "enable"
+ );
+
+ public static final Set<String> ALL_LINT_CODES = Set.of(
+ DRLLintHelper.CODE_MISSING_END,
+ DRLLintHelper.CODE_MISSING_SEPARATOR,
+ DRLLintHelper.CODE_MISSING_SEMICOLON,
+ DRLLintHelper.CODE_UNBALANCED_PARENS,
+ DRLLintHelper.CODE_MVEL_PROPERTY_ACCESS,
+ DRLLintHelper.CODE_UNKNOWN_TYPE,
+ DRLLintHelper.CODE_UNKNOWN_MEMBER
+ );
+
+ public static final Set<String> ALL_DEPRECATED_CODES = Set.of(
+ DeprecatedConstructs.CODE_HALF_CONSTRAINT,
+ DeprecatedConstructs.CODE_PATTERN_CONNECTIVE,
+ DeprecatedConstructs.CODE_LHS_CONNECTIVE_ANNOTATION,
+ DeprecatedConstructs.CODE_AGENDA_GROUP,
+ DeprecatedConstructs.CODE_CUSTOM_OPERATOR
+ );
+
+ public static final Set<String> ALL_KNOWN_CODES;
+ static {
+ Set<String> all = new HashSet<>(ALL_LINT_CODES);
+ all.addAll(ALL_DEPRECATED_CODES);
+ ALL_KNOWN_CODES = Collections.unmodifiableSet(all);
+ }
+
+ private DRLLintSuppression() {
+ }
+
+ /**
+ * Filters {@code diagnostics} against any {@code @drl-lsp-lint} directives
+ * found in {@code text}. Diagnostics with sources other than {@code
drools-lint},
+ * {@code drools-type}, or {@code drools-deprecated} are never dropped.
+ */
+ public static List<Diagnostic> filter(List<Diagnostic> diagnostics, String
text) {
Review Comment:
`filter`, `isSuppressed(Diagnostic)`, `hasAnySuppression` and
`isSuppressibleSource` are only used by tests now, and `parse(String)` only by
the code action
(https://github.com/apache/incubator-kie-tools/pull/4083#discussion_r4662942059)
and the old `DRLLintHelper` overloads. could the class just expose
`parse(tokens, text)`? the tests could get the index from
`DRLDiagnosticHelper.parse(text).suppression`, then they cover the tokens the
server really uses. the three code sets could be private too
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]