Skip to content

Commit

Permalink
Merge pull request #27 from otymko/feature/proto-selected
Browse files Browse the repository at this point in the history
Анализ и форматирование выделенного текста
  • Loading branch information
otymko authored Oct 10, 2019
2 parents fd16bfd + 9b621a0 commit 2719953
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 238 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Build with Gradle
run: ./gradlew build
- name: Copy file
run: cp ./build/libs/phoenix-0.2.jar ./.github/phoenix.jar
run: cp ./build/libs/phoenix-0.2.1.jar ./.github/phoenix.jar
- uses: actions/upload-artifact@master
with:
name: phoenix-0.1
Expand Down
5 changes: 3 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ repositories {
}

group = "org.github.otymko.phoenixbsl"
version = "0.2"
version = "0.2.1"

dependencies {

implementation("net.java.dev.jna:jna-platform:5.4.0")
compile("com.hynnet", "jacob", "1.18")
compile("com.github.mmarquee:ui-automation:develop-SNAPSHOT")
compile("com.github.1c-syntax:bsl-language-server:0.10.2")
Expand All @@ -32,7 +33,7 @@ dependencies {
}

configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_10
sourceCompatibility = JavaVersion.VERSION_11
}

tasks.withType<JavaCompile> {
Expand Down
174 changes: 43 additions & 131 deletions src/main/java/org/github/otymko/phoenixbsl/App.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.github.otymko.phoenixbsl;

import mmarquee.automation.AutomationException;
import mmarquee.automation.Element;
import mmarquee.automation.UIAutomation;
import mmarquee.automation.controls.Window;
import com.sun.jna.platform.win32.WinDef;
import org.eclipse.lsp4j.DocumentFormattingParams;
import org.eclipse.lsp4j.FormattingOptions;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration;
import org.github._1c_syntax.bsl.languageserver.context.ServerContext;
import org.github._1c_syntax.bsl.languageserver.providers.DiagnosticProvider;
import org.github._1c_syntax.bsl.languageserver.providers.FormatProvider;
import org.github.otymko.phoenixbsl.core.PhoenixAPI;
import org.github.otymko.phoenixbsl.core.PhoenixUser32;
import org.github.otymko.phoenixbsl.events.GlobalKeyboardHookHandler;
import org.github.otymko.phoenixbsl.views.IssuesForm;
import org.github.otymko.phoenixbsl.views.Toolbar;
Expand All @@ -34,17 +33,9 @@ public class App {

private final File fakeFile = new File(FAKE_PATH_FILE);
private Pattern pattern = Pattern.compile(REGEX_FORM_TITLE, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);

private UIAutomation automation;
private DiagnosticProvider diagnosticProvider;

private int thisIdProcess = 0;
private Window thisForm;
private Element focusElement;
private String tmpTextModule;


private IssuesForm issuesForm;
private WinDef.HWND focusForm;

public App() {
}
Expand All @@ -55,9 +46,6 @@ public static App getInstance() {

public void run() {

// инициализация UIAutomation
automation = UIAutomation.getInstance();

// инициализация для BSL LS
diagnosticProvider = new DiagnosticProvider(LanguageServerConfiguration.create());

Expand All @@ -81,155 +69,79 @@ public void abort() {
}

public boolean appIsRunning() {
var thisPid = ProcessHandle.current().pid();
AtomicBoolean isRunning = new AtomicBoolean(false);
ProcessHandle.allProcesses()
.filter(
ph -> ph.info().command().isPresent() && ph.info().command().get().contains("phoenixbsl"))
ph -> ph.info().command().isPresent()
&& ph.info().command().get().contains("phoenixbsl")
&& ph.pid() != thisPid)
.forEach((process) -> {
isRunning.set(true);
});
return isRunning.get();
}

public void startCheckBSL() {
public void runCheckBSL() {

var moduleText = getModuleText();
if (moduleText == null ) {
return;
var lineOfset = 0;
var textForCheck = "";
var textModuleSelected = PhoenixAPI.getTextSelected();
if (textModuleSelected.length() > 0) {
// получем номер строки
textForCheck = textModuleSelected;
lineOfset = PhoenixAPI.getCurrentLineNumber();
}
else {
textForCheck = PhoenixAPI.getTextAll();
}

var bslServerContext = new ServerContext();
var documentContext = bslServerContext.addDocument(fakeFile.toURI().toString(), moduleText);
var documentContext = bslServerContext.addDocument(fakeFile.toURI().toString(), textForCheck);
var list = diagnosticProvider.computeDiagnostics(documentContext);

issuesForm.setLineOfset(lineOfset);
issuesForm.updateIssues(list);
issuesForm.onVisible();
}

public void checkFocusForm() {

focusElement = null;
try {
focusElement = automation.getFocusedElement();
} catch (AutomationException e) {
log.error(e.getStackTrace().toString());
return;
}

var idProcess = 0;

if (focusElement == null) {
clearFocusCurrentForm();
} else {
try {
idProcess = focusElement.getProcessId().intValue();
if (focusElement.getControlType() == UIA_CONTROL_DOCUMENT) {
tmpTextModule = focusElement.getPropertyValue(UIA_PROPERTY_VALUE).toString();
}
else {
tmpTextModule = null;
}
} catch (AutomationException e) {
log.error(e.getStackTrace().toString());
}

try {
var finalIdProcess = idProcess;
automation.getDesktopWindows().forEach(window -> {
try {
if (window.getProcessId().toString().equals(String.valueOf(finalIdProcess))) {
thisForm = window;
}
} catch (AutomationException e) {
log.error(e.getStackTrace().toString());
}
});
} catch (AutomationException e) {
log.error(e.getStackTrace().toString());
}

try {
var txt = thisForm.getName();
var matcher = pattern.matcher(txt);
if (matcher.find()) {
thisIdProcess = idProcess;
} else {
clearFocusCurrentForm();
}
} catch (AutomationException e) {
clearFocusCurrentForm();
log.error(e.getStackTrace().toString());
}
}

log.info("This form: " + focusElement);
log.info("This id process " + thisIdProcess);
}

public void formattingTextByBSL() {
public void runFormattingBSL() {

var moduleText = getModuleText();
if (moduleText == null ) {
return;
var textForFormatting = "";
var isSelected = false;
var textModuleSelected = PhoenixAPI.getTextSelected();
if (textModuleSelected.length() > 0) {
textForFormatting = textModuleSelected;
isSelected = true;
}
else {
textForFormatting = PhoenixAPI.getTextAll();;
}

var bslServerContext = new ServerContext();
var params = new DocumentFormattingParams();
params.setTextDocument(getTextDocumentIdentifier(fakeFile));
params.setOptions(new FormattingOptions(4, true));
params.setOptions(new FormattingOptions(4, false));

var documentContext = bslServerContext.addDocument(fakeFile.toURI().toString(), moduleText);
var textEdits = FormatProvider.getFormatting(params, documentContext);
var documentContext = bslServerContext.addDocument(fakeFile.toURI().toString(), textForFormatting);
var newText = FormatProvider.getFormatting(params, documentContext);

var newModuleText = textEdits.get(0).getNewText();
PhoenixAPI.insetTextOnForm(newText.get(0).getNewText(), isSelected);
}

var customRobot = new CustomRobot();
customRobot.updateTextOnForm(newModuleText);
public void gotoLineModule(int line) {
log.info("Line: " + line);
PhoenixUser32.setFocusWindows(focusForm);
PhoenixAPI.goToLineOnForm(line);
}

public void updateFocusForm() {
focusForm = PhoenixUser32.getHWNDFocusForm();
}

private TextDocumentIdentifier getTextDocumentIdentifier(File file) {
var uri = file.toURI().toString();
return new TextDocumentIdentifier(uri);
}

private String getModuleText() {
String moduleText = null;
try {
moduleText = getTextDocument();
} catch (AutomationException e) {
log.error(e.getStackTrace().toString());
}
return moduleText;
}

private String getTextDocument() throws AutomationException {

var module = "";
if (tmpTextModule != null) {
module = tmpTextModule;
} else {
module = thisForm.getDocument(0).getElement().getPropertyValue(30045).toString();
}
return module;
}

private void clearFocusCurrentForm() {
thisForm = null;
thisIdProcess = 0;
}

public void focusDocumentLine(int line) {

log.info("Line: " + line);
focusElement.setFocus();
var customRobot = new CustomRobot();
customRobot.goToLineOnForm(line);

}

public boolean isFindForm() {
return thisIdProcess != 0;
}

}
Loading

0 comments on commit 2719953

Please sign in to comment.