Skip to content

Commit

Permalink
feat: #123 Изменение алгоритма работы выделения текста при анализе. Т…
Browse files Browse the repository at this point in the history
…еперь анализируется весь модуль. При выводе на форму issues - работает фильтр по выделенным строкам.
  • Loading branch information
otymko committed Feb 15, 2021
1 parent 00cf886 commit 2f8a472
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void showIssuesStage() {

@Override
public void updateIssues(List<Diagnostic> diagnostics) {
issuesStage.lineOffset = PhoenixCore.getInstance().getTextEditor().getCurrentOffset();
showIssuesStageImpl();
Platform.runLater(() -> issuesStage.updateIssues(diagnostics));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.otymko.phoenixbsl.PhoenixCore;
import com.github.otymko.phoenixbsl.gui.controller.IssueStageController;
import com.github.otymko.phoenixbsl.logic.PhoenixAPI;
import com.github.otymko.phoenixbsl.logic.utils.IssueHelper;
import com.github.otymko.phoenixbsl.model.Issue;
import com.github.otymko.phoenixbsl.model.ProjectSetting;
import com.jfoenix.assets.JFoenixResources;
Expand Down Expand Up @@ -59,8 +60,6 @@ public class IssuesStage extends Stage {
private final ComboBox<ProjectSetting> project;
private final TextField search;

public int lineOffset = 0;

private int countError = 0;
private int countWarning = 0;
private int countInfo = 0;
Expand Down Expand Up @@ -203,33 +202,21 @@ public IssuesStage(Stage ownerStage) {
}

public void updateIssues(List<Diagnostic> diagnostics) {
var textEditor = PhoenixCore.getInstance().getTextEditor();
var selection = textEditor.getSelection();

countError = 0;
countWarning = 0;
countInfo = 0;

issues.clear();
diagnostics.forEach(diagnostic -> {
var range = diagnostic.getRange();
var position = range.getStart();
var startLine = position.getLine() + 1 + lineOffset;

Issue issue = new Issue();

issue.setSource(PhoenixAPI.getValueSourceByString(diagnostic.getSource()));
issue.setDescription(diagnostic.getMessage());
issue.setStartLine(startLine);
issue.setLocation(String.valueOf(startLine));
issue.setSeverity(diagnostic.getSeverity());
issues.add(issue);

if (diagnostic.getSeverity() == DiagnosticSeverity.Error) {
countError++;
} else if (diagnostic.getSeverity() == DiagnosticSeverity.Warning) {
countWarning++;
} else {
countInfo++;
}
});

diagnostics.stream()
.filter(diagnostic -> IssueHelper.checkDiagnosticBySelection(diagnostic, selection))
.forEach(diagnostic -> {
var issue = IssueHelper.createIssue(diagnostic);
issues.add(issue);
calcCounters(diagnostic);
});

FXCollections.sort(issues, Comparator.comparingInt(Issue::getStartLine));
updateIndicators();
Expand All @@ -255,6 +242,16 @@ private void updateIndicators() {
labelInfo.setText("Инфо: " + countInfo);
}

private void calcCounters(Diagnostic diagnostic) {
if (diagnostic.getSeverity() == DiagnosticSeverity.Error) {
countError++;
} else if (diagnostic.getSeverity() == DiagnosticSeverity.Warning) {
countWarning++;
} else {
countInfo++;
}
}

private static Map<DiagnosticSeverity, String> createSeverityToStringMap() {
Map<DiagnosticSeverity, String> map = new EnumMap<>(DiagnosticSeverity.class);
map.put(DiagnosticSeverity.Error, "Ошибка");
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/github/otymko/phoenixbsl/logic/PhoenixAPI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.github.otymko.phoenixbsl.logic;

import com.github.otymko.phoenixbsl.logic.text.SourceText;
import com.github.otymko.phoenixbsl.logic.designer.DesignerTextEditor;
import com.github.otymko.phoenixbsl.logic.service.SonarLintService;
import com.github.otymko.phoenixbsl.logic.text.Constant;
import com.sun.jna.platform.win32.WinDef;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -19,7 +21,6 @@
@Slf4j
@UtilityClass
public class PhoenixAPI {
private final String FUN_SYMBOL = "☻"; // 9787
private final CustomRobot robot = new CustomRobot();
private final CustomTextTransfer textTransfer = new CustomTextTransfer();

Expand Down Expand Up @@ -66,23 +67,22 @@ public void goToLineOnForm(int line) {
robot.pressKey(KeyEvent.VK_ENTER);
}

public int getCurrentLineNumber() {

public SourceText getSourceText() {
var line = 0;
robot.Alt(KeyEvent.VK_NUMPAD2);
var textAll = getTextAll();
robot.Ctrl(KeyEvent.VK_Z);
String[] arrStr = textAll.split("\n");
String[] arrStr = textAll.split(Constant.SEPARATOR);
var count = 0;
for (var element : arrStr) {
count++;
if (element.contains(FUN_SYMBOL)) {
if (element.contains(Constant.FUN_SYMBOL)) {
line = count - 1;
break;
}
}
LOGGER.debug("Current line offset: " + line);
return line;
return new SourceText(textAll, line);
}

public String getTextAll() {
Expand Down Expand Up @@ -122,14 +122,14 @@ public void showMessageDialog(String message) {
}

public String applyFixForText(String textForQF, List<Either<Command, CodeAction>> codeActions) {
var strings = textForQF.split(DesignerTextEditor.SEPARATOR);
var strings = textForQF.split(Constant.SEPARATOR);
try {
applyAllQuickFixes(codeActions, strings);
} catch (ArrayIndexOutOfBoundsException e) {
LOGGER.error("При применении fix all к тексту модуля возникли ошибки", e);
return null;
}
return String.join(DesignerTextEditor.SEPARATOR, strings);
return String.join(Constant.SEPARATOR, strings);
}

public void clearListBySource(List<Diagnostic> diagnostics, String source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.github.otymko.phoenixbsl.logic.PhoenixUser32;
import com.github.otymko.phoenixbsl.logic.event.EventListener;
import com.github.otymko.phoenixbsl.logic.event.EventManager;
import com.github.otymko.phoenixbsl.logic.text.Location;
import com.github.otymko.phoenixbsl.logic.utils.TextUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -23,7 +25,6 @@
public class DesignerTextEditor implements EventListener {
public static final List<String> DIAGNOSTIC_FOR_QF = createDiagnosticListForQuickFix();
public static final List<String> FILTER_ACTION_QUICKFIX = createListFilterActionQuickFix();
public static final String SEPARATOR = "\n";
private final PhoenixCore core;
@Getter
@Setter
Expand All @@ -37,7 +38,7 @@ public class DesignerTextEditor implements EventListener {
private final List<Diagnostic> diagnostics = Collections.synchronizedList(new ArrayList<>());
@Getter
@Setter
private int currentOffset = 0;
private Location selection = Location.empty();

public DesignerTextEditor(PhoenixCore core) {
this.core = core;
Expand Down Expand Up @@ -124,16 +125,17 @@ public void updateFocusForm() {
}

private String getTextFormDesigner() {
setCurrentOffset(0);
var textForCheck = "";
var textModuleSelected = PhoenixAPI.getTextSelected();
var sourceText = PhoenixAPI.getSourceText();
var textForCheck = sourceText.getContent();

if (textModuleSelected.length() > 0) {
// получем номер строки
textForCheck = textModuleSelected;
setCurrentOffset(PhoenixAPI.getCurrentLineNumber());
setSelection(new Location(sourceText.getOffset() + 1,
sourceText.getOffset() + TextUtil.numberOfLinesInText(textModuleSelected)));
} else {
textForCheck = PhoenixAPI.getTextAll();
setSelection(Location.empty());
}
textForCheck = TextUtil.pasteSelectionInText(textForCheck, textModuleSelected);
return textForCheck;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.otymko.phoenixbsl.logic.text;

import lombok.experimental.UtilityClass;

@UtilityClass
public class Constant {
public final String SEPARATOR = "\n";
public final String FUN_SYMBOL = "☻"; // 9787
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.otymko.phoenixbsl.logic.text;

import lombok.RequiredArgsConstructor;
import lombok.Value;

@Value
@RequiredArgsConstructor
public class Location {
private static final Location EMPTY = new Location(0, 0);

int startLine;
int endLine;

public static Location empty() {
return EMPTY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.otymko.phoenixbsl.logic.text;

import lombok.RequiredArgsConstructor;
import lombok.Value;

@Value
@RequiredArgsConstructor
public class SourceText {
String content;
int offset;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.otymko.phoenixbsl.logic.utils;

import com.github.otymko.phoenixbsl.logic.PhoenixAPI;
import com.github.otymko.phoenixbsl.logic.text.Location;
import com.github.otymko.phoenixbsl.model.Issue;
import lombok.experimental.UtilityClass;
import org.eclipse.lsp4j.Diagnostic;

@UtilityClass
public class IssueHelper {

public boolean checkDiagnosticBySelection(Diagnostic diagnostic, Location selection) {
if (selection == Location.empty()) {
return true;
}
return diagnostic.getRange().getStart().getLine() >= selection.getStartLine()
&& diagnostic.getRange().getEnd().getLine() <= selection.getEndLine();
}

public Issue createIssue(Diagnostic diagnostic) {
var range = diagnostic.getRange();
var position = range.getStart();
var startLine = position.getLine() + 1;

var issue = new Issue();
issue.setSource(PhoenixAPI.getValueSourceByString(diagnostic.getSource()));
issue.setDescription(diagnostic.getMessage());
issue.setStartLine(startLine);
issue.setLocation(String.valueOf(startLine));
issue.setSeverity(diagnostic.getSeverity());
return issue;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.otymko.phoenixbsl.logic.utils;

import com.github.otymko.phoenixbsl.logic.text.Constant;
import lombok.experimental.UtilityClass;

@UtilityClass
public class TextUtil {

public String pasteSelectionInText(String text, String selection) {
return text.replace(Constant.FUN_SYMBOL, selection);
}

public int numberOfLinesInText(String text) {
return text.split(Constant.SEPARATOR).length;
}

}

0 comments on commit 2f8a472

Please sign in to comment.