Skip to content

Commit

Permalink
Merge pull request #107 from 1c-syntax/fix/highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
nixel2007 authored Sep 22, 2020
2 parents 2628d8e + e0da556 commit 83b2f00
Show file tree
Hide file tree
Showing 5 changed files with 1,140 additions and 150 deletions.
130 changes: 3 additions & 127 deletions src/main/java/com/github/_1c_syntax/bsl/sonar/BSLCoreSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticInfo;
import com.github._1c_syntax.bsl.languageserver.diagnostics.metadata.DiagnosticParameterInfo;
import com.github._1c_syntax.bsl.parser.BSLLexer;
import com.github._1c_syntax.bsl.sonar.language.BSLLanguage;
import com.github._1c_syntax.bsl.sonar.language.BSLLanguageServerRuleDefinition;
import com.github._1c_syntax.utils.Absolute;
Expand All @@ -50,16 +49,13 @@
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.batch.sensor.coverage.NewCoverage;
import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.FileLinesContext;
import org.sonar.api.measures.FileLinesContextFactory;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
import java.net.URI;
Expand All @@ -84,6 +80,7 @@ public class BSLCoreSensor implements Sensor {
private final boolean langServerEnabled;
private final List<String> sourcesList;
private final IssuesLoader issuesLoader;
private final BSLHighlighter highlighter;

private final boolean calculateCoverLoc;

Expand All @@ -105,6 +102,7 @@ public BSLCoreSensor(SensorContext context, FileLinesContextFactory fileLinesCon
.orElse(Collections.singletonList("."));

issuesLoader = new IssuesLoader(context);
highlighter = new BSLHighlighter(context);
}

@Override
Expand Down Expand Up @@ -195,7 +193,7 @@ private void processFile(InputFile inputFile, ServerContext bslServerContext) {
}

saveCpd(inputFile, documentContext);
saveHighlighting(inputFile, documentContext);
highlighter.saveHighlighting(inputFile, documentContext);
saveMeasures(inputFile, documentContext);

saveCoverageLoc(inputFile, documentContext);
Expand Down Expand Up @@ -230,34 +228,6 @@ private void saveCpd(InputFile inputFile, DocumentContext documentContext) {

}

private void saveHighlighting(InputFile inputFile, DocumentContext documentContext) {

NewHighlighting highlighting = context.newHighlighting().onFile(inputFile);

documentContext.getTokens().forEach((Token token) -> {
TypeOfText typeOfText = getTypeOfText(token.getType());

if (typeOfText == null) {
return;
}

int line = token.getLine();
int charPositionInLine = token.getCharPositionInLine();
String tokenText = token.getText();

highlighting.highlight(
line,
charPositionInLine,
line,
charPositionInLine + tokenText.length(),
typeOfText
);
});

highlighting.save();

}

private void saveMeasures(InputFile inputFile, DocumentContext documentContext) {

MetricStorage metrics = documentContext.getMetrics();
Expand Down Expand Up @@ -401,100 +371,6 @@ private LanguageServerConfiguration getLanguageServerConfiguration() {
return configuration;
}

@Nullable
private static TypeOfText getTypeOfText(int tokenType) {

TypeOfText typeOfText = null;

switch (tokenType) {
case BSLLexer.PROCEDURE_KEYWORD:
case BSLLexer.FUNCTION_KEYWORD:
case BSLLexer.ENDPROCEDURE_KEYWORD:
case BSLLexer.ENDFUNCTION_KEYWORD:
case BSLLexer.EXPORT_KEYWORD:
case BSLLexer.VAL_KEYWORD:
case BSLLexer.ENDIF_KEYWORD:
case BSLLexer.ENDDO_KEYWORD:
case BSLLexer.IF_KEYWORD:
case BSLLexer.ELSIF_KEYWORD:
case BSLLexer.ELSE_KEYWORD:
case BSLLexer.THEN_KEYWORD:
case BSLLexer.WHILE_KEYWORD:
case BSLLexer.DO_KEYWORD:
case BSLLexer.FOR_KEYWORD:
case BSLLexer.TO_KEYWORD:
case BSLLexer.EACH_KEYWORD:
case BSLLexer.IN_KEYWORD:
case BSLLexer.TRY_KEYWORD:
case BSLLexer.EXCEPT_KEYWORD:
case BSLLexer.ENDTRY_KEYWORD:
case BSLLexer.RETURN_KEYWORD:
case BSLLexer.CONTINUE_KEYWORD:
case BSLLexer.RAISE_KEYWORD:
case BSLLexer.VAR_KEYWORD:
case BSLLexer.NOT_KEYWORD:
case BSLLexer.OR_KEYWORD:
case BSLLexer.AND_KEYWORD:
case BSLLexer.NEW_KEYWORD:
case BSLLexer.GOTO_KEYWORD:
case BSLLexer.BREAK_KEYWORD:
case BSLLexer.EXECUTE_KEYWORD:
typeOfText = TypeOfText.KEYWORD;
break;
case BSLLexer.TRUE:
case BSLLexer.FALSE:
case BSLLexer.UNDEFINED:
case BSLLexer.NULL:
case BSLLexer.DATETIME:
case BSLLexer.DECIMAL:
case BSLLexer.FLOAT:
typeOfText = TypeOfText.CONSTANT;
break;
case BSLLexer.STRING:
case BSLLexer.STRINGSTART:
case BSLLexer.STRINGPART:
case BSLLexer.STRINGTAIL:
case BSLLexer.PREPROC_STRING:
typeOfText = TypeOfText.STRING;
break;
case BSLLexer.LINE_COMMENT:
typeOfText = TypeOfText.COMMENT;
break;
case BSLLexer.HASH:
case BSLLexer.PREPROC_USE_KEYWORD:
case BSLLexer.PREPROC_REGION:
case BSLLexer.PREPROC_END_REGION:
case BSLLexer.PREPROC_AND_KEYWORD:
case BSLLexer.PREPROC_OR_KEYWORD:
case BSLLexer.PREPROC_NOT_KEYWORD:
case BSLLexer.PREPROC_IF_KEYWORD:
case BSLLexer.PREPROC_THEN_KEYWORD:
case BSLLexer.PREPROC_ELSIF_KEYWORD:
case BSLLexer.PREPROC_ELSE_KEYWORD:
case BSLLexer.PREPROC_ENDIF_KEYWORD:
typeOfText = TypeOfText.PREPROCESS_DIRECTIVE;
break;
case BSLLexer.AMPERSAND:
case BSLLexer.ANNOTATION_AFTER_SYMBOL:
case BSLLexer.ANNOTATION_AROUND_SYMBOL:
case BSLLexer.ANNOTATION_ATCLIENT_SYMBOL:
case BSLLexer.ANNOTATION_ATCLIENTATSERVER_SYMBOL:
case BSLLexer.ANNOTATION_ATCLIENTATSERVERNOCONTEXT_SYMBOL:
case BSLLexer.ANNOTATION_ATSERVER_SYMBOL:
case BSLLexer.ANNOTATION_ATSERVERNOCONTEXT_SYMBOL:
case BSLLexer.ANNOTATION_BEFORE_SYMBOL:
case BSLLexer.ANNOTATION_CHANGEANDVALIDATE_SYMBOL:
case BSLLexer.ANNOTATION_CUSTOM_SYMBOL:
typeOfText = TypeOfText.ANNOTATION;
break;
default:
// no-op
}

return typeOfText;

}

private static Object castDiagnosticParameterValue(String valueToCast, Class<?> type) {
Object value;
if (type == Integer.class) {
Expand Down
Loading

0 comments on commit 83b2f00

Please sign in to comment.