-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Peter Vanusanik
committed
May 8, 2023
1 parent
2151b15
commit da7213e
Showing
18 changed files
with
444 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/com/en_circle/slt/plugin/actions/Macroexpand1Action.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.en_circle.slt.plugin.actions; | ||
|
||
import com.en_circle.slt.plugin.SltBundle; | ||
import com.en_circle.slt.plugin.lisp.lisp.LispString; | ||
import com.en_circle.slt.plugin.lisp.lisp.LispUtils; | ||
import com.en_circle.slt.plugin.lisp.psi.LispList; | ||
import com.en_circle.slt.plugin.services.lisp.LispEnvironmentService; | ||
import com.en_circle.slt.plugin.swank.requests.Macroexpand1; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.Messages; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class Macroexpand1Action extends MacroexpandActionBase { | ||
private static final Logger log = LoggerFactory.getLogger(Macroexpand1Action.class); | ||
|
||
@Override | ||
protected void macroexpand(Project project, LispList form, String packageName, MacroexpandCallback callback) { | ||
try { | ||
LispEnvironmentService service = LispEnvironmentService.getInstance(project); | ||
service.sendToLisp(new Macroexpand1(form.getText(), packageName, result -> { | ||
callback.showMacroexpand(LispUtils.unescape(((LispString) result).getValue())); | ||
}), true); | ||
} catch (Exception e) { | ||
log.warn(SltBundle.message("slt.error.start"), e); | ||
Messages.showErrorDialog(project, e.getMessage(), SltBundle.message("slt.ui.errors.lisp.start")); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/en_circle/slt/plugin/actions/MacroexpandAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.en_circle.slt.plugin.actions; | ||
|
||
import com.en_circle.slt.plugin.SltBundle; | ||
import com.en_circle.slt.plugin.lisp.lisp.LispString; | ||
import com.en_circle.slt.plugin.lisp.lisp.LispUtils; | ||
import com.en_circle.slt.plugin.lisp.psi.LispList; | ||
import com.en_circle.slt.plugin.services.lisp.LispEnvironmentService; | ||
import com.en_circle.slt.plugin.swank.requests.Macroexpand; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.Messages; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class MacroexpandAction extends MacroexpandActionBase { | ||
private static final Logger log = LoggerFactory.getLogger(MacroexpandAction.class); | ||
|
||
@Override | ||
protected void macroexpand(Project project, LispList form, String packageName, MacroexpandCallback callback) { | ||
try { | ||
LispEnvironmentService service = LispEnvironmentService.getInstance(project); | ||
service.sendToLisp(new Macroexpand(form.getText(), packageName, result -> { | ||
callback.showMacroexpand(LispUtils.unescape(((LispString) result).getValue())); | ||
}), true); | ||
} catch (Exception e) { | ||
log.warn(SltBundle.message("slt.error.start"), e); | ||
Messages.showErrorDialog(project, e.getMessage(), SltBundle.message("slt.ui.errors.lisp.start")); | ||
} | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
src/main/java/com/en_circle/slt/plugin/actions/MacroexpandActionBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.en_circle.slt.plugin.actions; | ||
|
||
import com.en_circle.slt.plugin.SltBundle; | ||
import com.en_circle.slt.plugin.SltCommonLispFileType; | ||
import com.en_circle.slt.plugin.environment.LispFeatures; | ||
import com.en_circle.slt.plugin.lisp.LispParserUtil; | ||
import com.en_circle.slt.plugin.lisp.psi.LispList; | ||
import com.en_circle.slt.plugin.services.lisp.LispEnvironmentService; | ||
import com.intellij.openapi.actionSystem.ActionUpdateThread; | ||
import com.intellij.openapi.actionSystem.AnAction; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
import com.intellij.openapi.editor.Document; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.openapi.editor.ex.EditorEx; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.popup.JBPopup; | ||
import com.intellij.openapi.ui.popup.JBPopupFactory; | ||
import com.intellij.openapi.util.text.HtmlBuilder; | ||
import com.intellij.openapi.util.text.HtmlChunk; | ||
import com.intellij.psi.PsiDocumentManager; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiFile; | ||
import org.apache.commons.lang.StringEscapeUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.swing.*; | ||
import javax.swing.text.html.HTMLEditorKit; | ||
import java.awt.*; | ||
import java.util.Objects; | ||
|
||
public abstract class MacroexpandActionBase extends AnAction { | ||
|
||
protected abstract void macroexpand(Project project, LispList form, String packageName, MacroexpandCallback callback); | ||
|
||
@Override | ||
public void actionPerformed(@NotNull AnActionEvent event) { | ||
EditorEx editor = (EditorEx) event.getData(CommonDataKeys.EDITOR); | ||
LispList list = null; | ||
|
||
if (editor != null) { | ||
PsiDocumentManager psiMgr = PsiDocumentManager.getInstance(Objects.requireNonNull(editor.getProject())); | ||
psiMgr.commitDocument(editor.getDocument()); | ||
PsiFile psiFile = psiMgr.getPsiFile(editor.getDocument()); | ||
if (psiFile != null) { | ||
int caretOffset = editor.getExpectedCaretOffset(); | ||
list = findList(psiFile, caretOffset, editor.getDocument()); | ||
if (list != null) { | ||
editor.getSelectionModel().setSelection(list.getTextOffset(), list.getTextOffset() + list.getTextLength()); | ||
} | ||
} | ||
|
||
if (list != null) { | ||
int offset = list.getTextOffset(); | ||
String packageName = LispParserUtil.getPackage(psiFile, offset); | ||
macroexpand(editor.getProject(), list, packageName, text -> SwingUtilities.invokeLater(() -> { | ||
HtmlBuilder builder = new HtmlBuilder(); | ||
String macroExpand = StringUtils.replace(StringUtils.replace(StringEscapeUtils.escapeHtml(text), " ", " "), | ||
"\n", HtmlChunk.br().toString()); | ||
builder.append(HtmlChunk.raw(macroExpand)); | ||
|
||
JEditorPane editorPane = new JEditorPane("text/html", ""); | ||
editorPane.setEditorKit(new HTMLEditorKit()); | ||
editorPane.setText(builder.toString()); | ||
|
||
JPanel textPanel = new JPanel(new BorderLayout()); | ||
textPanel.add(editorPane, BorderLayout.CENTER); | ||
|
||
JBPopup popup = JBPopupFactory.getInstance() | ||
.createComponentPopupBuilder(textPanel, null) | ||
.setProject(editor.getProject()) | ||
.setTitle(SltBundle.message("slt.documentation.macroexpand")) | ||
.setShowBorder(true) | ||
.setMovable(true) | ||
.setFocusable(true) | ||
.setRequestFocus(true) | ||
.createPopup(); | ||
popup.showInBestPositionFor(editor.getDataContext()); | ||
})); | ||
} | ||
} | ||
} | ||
|
||
protected LispList findList(PsiFile psiFile, int caretOffset, Document document) { | ||
PsiElement element = psiFile.findElementAt(caretOffset); | ||
if (element != null) { | ||
PsiElement parent = element; | ||
while (parent != null) { | ||
parent = parent.getParent(); | ||
if (parent instanceof LispList) { | ||
return (LispList) parent; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void update(@NotNull AnActionEvent event) { | ||
event.getPresentation().setEnabledAndVisible(false); | ||
Editor editor = event.getData(CommonDataKeys.EDITOR); | ||
if (editor != null && event.getProject() != null) { | ||
PsiFile file = PsiDocumentManager.getInstance(Objects.requireNonNull(editor.getProject())).getPsiFile(editor.getDocument()); | ||
if (file != null && SltCommonLispFileType.INSTANCE.equals(file.getFileType())) { | ||
event.getPresentation().setEnabledAndVisible(LispEnvironmentService.getInstance(event.getProject()) | ||
.hasFeature(LispFeatures.MACROEXPAND)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public @NotNull ActionUpdateThread getActionUpdateThread() { | ||
return ActionUpdateThread.BGT; | ||
} | ||
|
||
protected interface MacroexpandCallback { | ||
|
||
void showMacroexpand(String text); | ||
|
||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/en_circle/slt/plugin/actions/MacroexpandAllAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.en_circle.slt.plugin.actions; | ||
|
||
import com.en_circle.slt.plugin.SltBundle; | ||
import com.en_circle.slt.plugin.lisp.lisp.LispString; | ||
import com.en_circle.slt.plugin.lisp.lisp.LispUtils; | ||
import com.en_circle.slt.plugin.lisp.psi.LispList; | ||
import com.en_circle.slt.plugin.services.lisp.LispEnvironmentService; | ||
import com.en_circle.slt.plugin.swank.requests.MacroexpandAll; | ||
import com.intellij.openapi.project.Project; | ||
import com.intellij.openapi.ui.Messages; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class MacroexpandAllAction extends MacroexpandActionBase { | ||
private static final Logger log = LoggerFactory.getLogger(MacroexpandAllAction.class); | ||
|
||
@Override | ||
protected void macroexpand(Project project, LispList form, String packageName, MacroexpandCallback callback) { | ||
try { | ||
LispEnvironmentService service = LispEnvironmentService.getInstance(project); | ||
service.sendToLisp(new MacroexpandAll(form.getText(), packageName, result -> { | ||
callback.showMacroexpand(LispUtils.unescape(((LispString) result).getValue())); | ||
}), true); | ||
} catch (Exception e) { | ||
log.warn(SltBundle.message("slt.error.start"), e); | ||
Messages.showErrorDialog(project, e.getMessage(), SltBundle.message("slt.ui.errors.lisp.start")); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/en_circle/slt/plugin/actions/MacroexpandGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.en_circle.slt.plugin.actions; | ||
|
||
import com.en_circle.slt.plugin.SltBundle; | ||
import com.en_circle.slt.plugin.SltCommonLispFileType; | ||
import com.en_circle.slt.plugin.environment.LispFeatures; | ||
import com.en_circle.slt.plugin.services.lisp.LispEnvironmentService; | ||
import com.intellij.openapi.actionSystem.ActionUpdateThread; | ||
import com.intellij.openapi.actionSystem.AnActionEvent; | ||
import com.intellij.openapi.actionSystem.CommonDataKeys; | ||
import com.intellij.openapi.actionSystem.DefaultActionGroup; | ||
import com.intellij.openapi.editor.Editor; | ||
import com.intellij.psi.PsiDocumentManager; | ||
import com.intellij.psi.PsiFile; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.Objects; | ||
|
||
public class MacroexpandGroup extends DefaultActionGroup { | ||
|
||
public MacroexpandGroup() { | ||
super(() -> SltBundle.message("action.slt.actions.macroexpand.group.text"), true); | ||
} | ||
|
||
@Override | ||
public void update(@NotNull AnActionEvent event) { | ||
event.getPresentation().setEnabledAndVisible(false); | ||
Editor editor = event.getData(CommonDataKeys.EDITOR); | ||
if (editor != null && event.getProject() != null) { | ||
PsiFile file = PsiDocumentManager.getInstance(Objects.requireNonNull(editor.getProject())).getPsiFile(editor.getDocument()); | ||
if (file != null && SltCommonLispFileType.INSTANCE.equals(file.getFileType())) { | ||
event.getPresentation().setEnabledAndVisible(LispEnvironmentService.getInstance(event.getProject()) | ||
.hasFeature(LispFeatures.MACROEXPAND)); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public @NotNull ActionUpdateThread getActionUpdateThread() { | ||
return ActionUpdateThread.BGT; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.