All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github._1c_syntax.bsl.languageserver.diagnostics.CommentedCodeDiagnostic Maven / Gradle / Ivy

Go to download

Language Server Protocol implementation for 1C (BSL) - 1C:Enterprise 8 and OneScript languages.

There is a newer version: 0.24.0-rc.1
Show newest version
/*
 * This file is a part of BSL Language Server.
 *
 * Copyright (c) 2018-2024
 * Alexey Sosnoviy , Nikita Fedkin  and contributors
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 *
 * BSL Language Server is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * BSL Language Server is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BSL Language Server.
 */
package com.github._1c_syntax.bsl.languageserver.diagnostics;

import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol;
import com.github._1c_syntax.bsl.languageserver.context.symbol.description.MethodDescription;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticMetadata;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticParameter;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticSeverity;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticTag;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticType;
import com.github._1c_syntax.bsl.languageserver.providers.CodeActionProvider;
import com.github._1c_syntax.bsl.languageserver.recognizer.BSLFootprint;
import com.github._1c_syntax.bsl.languageserver.recognizer.CodeRecognizer;
import com.github._1c_syntax.bsl.parser.BSLParser;
import com.github._1c_syntax.bsl.parser.BSLTokenizer;
import org.antlr.v4.runtime.Token;
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.CodeActionParams;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.TextEdit;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

@DiagnosticMetadata(
  type = DiagnosticType.CODE_SMELL,
  severity = DiagnosticSeverity.MINOR,
  minutesToFix = 1,
  tags = {
    DiagnosticTag.STANDARD,
    DiagnosticTag.BADPRACTICE
  }
)
public class CommentedCodeDiagnostic extends AbstractDiagnostic implements QuickFixProvider {

  private static final float COMMENTED_CODE_THRESHOLD = 0.9F;
  private static final String COMMENT_START = "//";
  private static final int MINIMAL_TOKEN_COUNT = 2;

  @DiagnosticParameter(
    type = Float.class,
    defaultValue = "" + COMMENTED_CODE_THRESHOLD
  )
  private float threshold = COMMENTED_CODE_THRESHOLD;

  @DiagnosticParameter(
    type = String.class
  )
  private Set exclusionPrefixes = Collections.emptySet();

  private List methodDescriptions;
  private CodeRecognizer codeRecognizer;

  public CommentedCodeDiagnostic() {
    codeRecognizer = new CodeRecognizer(threshold, new BSLFootprint());
  }

  @Override
  public void configure(Map configuration) {
    threshold = ((Number) configuration.getOrDefault("threshold", threshold)).floatValue();
    codeRecognizer = new CodeRecognizer(threshold, new BSLFootprint());

    String excludePrefixesString = (String) configuration.getOrDefault("exclusionPrefixes", "");
    exclusionPrefixes = Arrays.stream(excludePrefixesString.split(","))
      .map(String::trim)
      .filter(s -> !s.isEmpty())
      .collect(Collectors.toSet());
  }

  @Override
  public void check() {
    methodDescriptions = documentContext.getSymbolTree().getMethods()
      .stream()
      .map(MethodSymbol::getDescription)
      .filter(Optional::isPresent)
      .map(Optional::get)
      .collect(Collectors.toList());

    groupComments(documentContext.getComments())
      .stream()
      .filter(this::isCommentGroupNotMethodDescription)
      .forEach(this::checkCommentGroup);
  }

  private List> groupComments(List comments) {
    List> groups = new ArrayList<>();
    List currentGroup = null;

    for (Token comment : comments) {
      if (currentGroup == null) {
        currentGroup = initNewGroup(comment);
      } else if (isAdjacent(comment, currentGroup)) {
        currentGroup.add(comment);
      } else {
        groups.add(currentGroup);
        currentGroup = initNewGroup(comment);
      }
    }

    if (currentGroup != null) {
      groups.add(currentGroup);
    }

    return groups;
  }

  private static List initNewGroup(Token comment) {
    List group = new ArrayList<>();
    group.add(comment);
    return group;
  }

  private boolean isAdjacent(Token comment, List currentGroup) {

    Token last = currentGroup.get(currentGroup.size() - 1);
    return last.getLine() + 1 == comment.getLine()
      && onlyEmptyDelimiters(last.getTokenIndex(), comment.getTokenIndex());

  }

  private boolean onlyEmptyDelimiters(int firstTokenIndex, int lastTokenIndex) {
    if (firstTokenIndex > lastTokenIndex) {
      return false;
    }

    for (int index = firstTokenIndex + 1; index < lastTokenIndex; index++) {
      int tokenType = documentContext.getTokens().get(index).getType();
      if (tokenType != BSLParser.WHITE_SPACE) {
        return false;
      }
    }

    return true;
  }

  private boolean isCommentGroupNotMethodDescription(List commentGroup) {
    if (methodDescriptions.isEmpty()) {
      return true;
    }

    final Token first = commentGroup.get(0);
    final Token last = commentGroup.get(commentGroup.size() - 1);

    return methodDescriptions.stream().noneMatch(methodDescription -> methodDescription.contains(first, last));
  }

  private void checkCommentGroup(List commentGroup) {
    Token firstComment = commentGroup.get(0);
    Token lastComment = commentGroup.get(commentGroup.size() - 1);

    for (Token comment : commentGroup) {
      if (isTextParsedAsCode(comment.getText())) {
        diagnosticStorage.addDiagnostic(firstComment, lastComment);
        return;
      }
    }
  }

  private boolean isTextParsedAsCode(String text) {
    String uncommented = uncomment(text);

    for (String prefix : exclusionPrefixes) {
      if (uncommented.startsWith(prefix)) {
        return false;
      }
    }
    if (!codeRecognizer.meetsCondition(text)) {
      return false;
    }

    BSLTokenizer tokenizer = new BSLTokenizer(uncommented);
    final List tokens = tokenizer.getTokens();

    // Если меньше двух токенов нет смысла анализировать - это код
    if (tokens.size() >= MINIMAL_TOKEN_COUNT) {

      List tokenTypes = tokens.stream()
        .map(Token::getType)
        .filter(t -> t != BSLParser.WHITE_SPACE)
        .collect(Collectors.toList());

      // Если два идентификатора идут подряд - это не код
      for (int i = 0; i < tokenTypes.size() - 1; i++) {
        if (tokenTypes.get(i) == BSLParser.IDENTIFIER && tokenTypes.get(i + 1) == BSLParser.IDENTIFIER) {
          return false;
        }
      }
    }

    return true;
  }

  private static String uncomment(String comment) {
    if (comment.startsWith(COMMENT_START)) {
      return uncomment(comment.substring(COMMENT_START.length()));
    }
    return comment;
  }

  @Override
  public List getQuickFixes(
    List diagnostics, CodeActionParams params, DocumentContext documentContext
  ) {

    List textEdits = diagnostics.stream()
      .map(Diagnostic::getRange)
      .map(range -> new TextEdit(range, ""))
      .collect(Collectors.toList());

    return CodeActionProvider.createCodeActions(
      textEdits,
      info.getResourceString("quickFixMessage"),
      documentContext.getUri(),
      diagnostics
    );
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy