Skip to content

Commit

Permalink
1.0.14 (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
acharneski authored Mar 10, 2023
1 parent ac82b99 commit 11f5aed
Show file tree
Hide file tree
Showing 17 changed files with 1,384 additions and 847 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
### Added
-

## [1.0.14]

### Fixed
- Various issues with "Insert Implementation" action

## [1.0.13]

### Removed
Expand Down
8 changes: 5 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ pluginGroup = com.github.simiacryptus
pluginName = intellij-aicoder
pluginRepositoryUrl = https://github.com/SimiaCryptus/intellij-aicoder
# SemVer format -> https://semver.org
pluginVersion = 1.0.13
pluginVersion = 1.0.14

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 203
pluginUntilBuild = 223.*
pluginUntilBuild = 231.*

# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension

Expand All @@ -24,6 +24,7 @@ pluginUntilBuild = 223.*
platformType = IU
platformPlugins = com.intellij.java, org.intellij.scala:2021.3.22, Pythonid:213.7172.26, org.jetbrains.plugins.go:213.7172.6
#platformPlugins = com.intellij.java, org.intellij.scala:2022.3.16, Pythonid:223.8214.52, org.jetbrains.plugins.go:223.8214.52
#platformPlugins = com.intellij.java

# PhpStorm
#platformType = PS
Expand All @@ -33,11 +34,12 @@ platformPlugins = com.intellij.java, org.intellij.scala:2021.3.22, Pythonid:213.
#platformType = RD
#platformPlugins = JavaScript

# https://mvnrepository.com/artifact/com.jetbrains.intellij.idea/ideaIU
platformVersion = 2021.3.3
#platformVersion = 2022.3.1

# Gradle Releases -> https://github.com/gradle/gradle/releases
gradleVersion = 7.5.1
gradleVersion = 7.6.1

# Opt-out flag for bundling Kotlin standard library -> https://plugins.jetbrains.com/docs/intellij/kotlin.html#kotlin-standard-library
# suppress inspection "UnusedProperty"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class DescribeAction : AnAction() {
UITools.redoableRequest(request, indent, event,
{ newText ->
val wrapping = StringTools.lineWrapping(
newText!!.toString().trim { it <= ' ' }, 120
newText.toString().trim { it <= ' ' }, 120
)
val numberOfLines = wrapping.trim { it <= ' ' }.split("\n".toRegex()).dropLastWhile { it.isEmpty() }
.toTypedArray().size
Expand All @@ -86,7 +86,7 @@ class DescribeAction : AnAction() {
editor.document,
selectionStart,
selectionEnd,
newText!!
newText
)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ class DocAction : AnAction() {
val document = event.getRequiredData(CommonDataKeys.EDITOR).document
redoableRequest(completionRequest, "", event,
{ docString ->
language.docComment!!.fromString(docString.toString().trim { it <= ' ' })!!.withIndent(indent)
language.docComment.fromString(docString.toString().trim { it <= ' ' })!!.withIndent(indent)
.toString() + "\n" + indent + StringTools.trimPrefix(indentedInput.toString())
},
{ docString ->
replaceString(
document, startOffset, endOffset,
docString!!
docString
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ class PsiClassContextAction : AnAction() {
.filter { x: String -> !x.isEmpty() }
.reduce { a: String, b: String -> "$a $b" }.get()
val endOffset = psiClassContextActionParams.largestIntersectingComment.textRange.endOffset
val psiClassContext = PsiClassContext.getContext(
psiClassContextActionParams.psiFile,
psiClassContextActionParams.selectionStart,
psiClassContextActionParams.selectionEnd,
computerLanguage
)
val request = settings.createTranslationRequest()
.setInstruction("Implement " + humanLanguage + " as " + computerLanguage.name + " code")
.setInputType(humanLanguage)
Expand All @@ -54,13 +60,7 @@ class PsiClassContextAction : AnAction() {
.buildCompletionRequest()
.appendPrompt(
"""
${
PsiClassContext.getContext(
psiClassContextActionParams.psiFile,
psiClassContextActionParams.selectionStart,
psiClassContextActionParams.selectionEnd
)
}
$psiClassContext
""".trimIndent()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class RenameVariablesAction : AnAction() {
@NotNull val mainCursor = caretModel.primaryCaret
@NotNull val outputLanguage = AppSettingsState.getInstance().humanLanguage
val sourceFile = actionEvent.getRequiredData(CommonDataKeys.PSI_FILE)
val codeElement = PsiUtil.getSmallestIntersectingMajorCodeElement(sourceFile, mainCursor) ?: return
val codeElement = PsiUtil.getSmallestIntersectingMajorCodeElement(sourceFile, mainCursor) ?: throw IllegalStateException()
@NotNull val programmingLanguage = ComputerLanguage.getComputerLanguage(actionEvent)
val appSettings = AppSettingsState.getInstance()

Expand All @@ -61,7 +61,7 @@ class RenameVariablesAction : AnAction() {
val textIndent = UITools.getIndent(textCursor)
UITools.redoableRequest(completionRequest, textIndent, actionEvent) { completionText ->

val renameSuggestions = completionText!!.split('\n').stream().map { x ->
val renameSuggestions = completionText.split('\n').stream().map { x ->
val kv = StringTools.stripSuffix(StringTools.stripPrefix(x.trim(), "|"), "|").split('|')
.map { x -> x.trim() }
kv[0] to kv[1]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ class RewordCommentAction : AnAction() {
{ newText ->
indent.toString() + commentModel!!.fromString(
StringTools.lineWrapping(
newText!!, 120
newText, 120
)
)!!.withIndent(indent)
},{ newText ->
UITools.replaceString(
document, startOffset, endOffset,
newText!!
newText
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@ class FactCheckLinkedListAction : AnAction() {
val settings = AppSettingsState.getInstance()
val caret = event.getRequiredData(CommonDataKeys.EDITOR).caretModel.primaryCaret
val languageName = ComputerLanguage.getComputerLanguage(event)!!.name
val endOffset: Int
val startOffset: Int
val psiFile = PsiUtil.getPsiFile(event)!!
val elements = PsiUtil.getAllIntersecting(psiFile, caret.selectionStart, caret.selectionEnd, "ListItem")
val elementText = elements.flatMap { it.children.map { it.text } }.toTypedArray()
startOffset = elements.minByOrNull { it.startOffset }?.startOffset ?: caret.selectionStart
endOffset = elements.maxByOrNull { it.endOffset }?.endOffset ?: caret.selectionEnd
val startOffset = elements.minByOrNull { it.startOffset }?.startOffset ?: caret.selectionStart
val endOffset = elements.maxByOrNull { it.endOffset }?.endOffset ?: caret.selectionEnd
val replaceString = event.getRequiredData(CommonDataKeys.EDITOR).document.text.substring(startOffset, endOffset)
val completionRequest = settings.createTranslationRequest()
.setInstruction(getInstruction("Translate each item into a search query that can be used to fact check each item with a search engine"))
Expand All @@ -43,7 +41,7 @@ class FactCheckLinkedListAction : AnAction() {
val document = event.getRequiredData(CommonDataKeys.EDITOR).document
redoableRequest(completionRequest, "", event,
{ newText ->
val queries = newText!!.replace("\"".toRegex(), "").split("\n\\d+\\.\\s+".toRegex()).toTypedArray()
val queries = newText.replace("\"".toRegex(), "").split("\n\\d+\\.\\s+".toRegex()).toTypedArray()
if (queries.size != elementText.size) {
throw RuntimeException("Invalid response: " + newText)
}
Expand All @@ -55,7 +53,7 @@ class FactCheckLinkedListAction : AnAction() {
}
},
{ newText ->
replaceString(document, startOffset, endOffset, newText!!)
replaceString(document, startOffset, endOffset, newText)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class MarkdownNewTableColsAction : AnAction() {
event.getRequiredData(CommonDataKeys.EDITOR)
.document,
markdownNewTableColsParams.table.textRange.startOffset,
markdownNewTableColsParams.table.textRange.endOffset, newText!!
markdownNewTableColsParams.table.textRange.endOffset, newText
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class MarkdownNewTableRowsAction : AnAction() {
"",
event,
{ transformCompletion(markdownNewTableRowsParams, it) },
{ UITools.insertString(document, endOffset, it!!) })
{ UITools.insertString(document, endOffset, it) })
}

class MarkdownNewTableRowsParams constructor(val caret: Caret, val table: PsiElement)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("UNNECESSARY_SAFE_CALL")

package com.github.simiacryptus.aicoder.util

import com.github.simiacryptus.aicoder.openai.OpenAI_API
Expand Down Expand Up @@ -83,7 +85,7 @@ object UITools {
event: AnActionEvent,
transformCompletion: Function<CharSequence, CharSequence>,
action: Function<CharSequence, Runnable>,
resultFuture: ListenableFuture<CharSequence> = OpenAI_API.complete(event.project!!, request, indent?:""),
resultFuture: ListenableFuture<CharSequence> = OpenAI_API.complete(event.project!!, request, indent),
progressIndicator: ProgressIndicator? = startProgress()
) {
Futures.addCallback(resultFuture, object : FutureCallback<CharSequence?> {
Expand Down Expand Up @@ -168,7 +170,7 @@ object UITools {
val editor = event.getData(CommonDataKeys.EDITOR)
val document = Objects.requireNonNull(editor)!!.document
val progressIndicator = startProgress()
val resultFuture = OpenAI_API.edit(event.project!!, request.uiIntercept(), indent!!)
val resultFuture = OpenAI_API.edit(event.project!!, request.uiIntercept(), indent)
Futures.addCallback(resultFuture, object : FutureCallback<CharSequence?> {
override fun onSuccess(result: CharSequence?) {
progressIndicator?.cancel()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.simiacryptus.aicoder.util.psi

import com.github.simiacryptus.aicoder.util.ComputerLanguage
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiFile
import java.util.ArrayList

class PsiClassContext(val text: String, val isPrior: Boolean, val isOverlap: Boolean) {
class PsiClassContext(val text: String, val isPrior: Boolean, val isOverlap: Boolean, val language: ComputerLanguage) {
val children = ArrayList<PsiClassContext>()

/**
Expand Down Expand Up @@ -36,22 +37,36 @@ class PsiClassContext(val text: String, val isPrior: Boolean, val isOverlap: Boo
val within =
textRangeStartOffset <= selectionStart && textRangeEndOffset > selectionStart && textRangeStartOffset <= selectionEnd && textRangeEndOffset > selectionEnd
if (PsiUtil.matchesType(element, "ImportList")) {
currentContext.children.add(PsiClassContext(text.trim { it <= ' ' }, isPrior, isOverlap))
currentContext.children.add(PsiClassContext(text.trim { it <= ' ' }, isPrior, isOverlap, language))
} else if (PsiUtil.matchesType(element, "Comment", "DocComment")) {
if (within) {
currentContext.children.add(PsiClassContext(indent + text.trim { it <= ' ' }, false, true))
currentContext.children.add(PsiClassContext(indent + text.trim { it <= ' ' }, false, true, language))
}
} else if (PsiUtil.matchesType(element, "Method", "Field")) {
} else if (PsiUtil.matchesType(element, "Field")) {
processChildren(
element,
self,
isPrior,
isOverlap,
indent + PsiUtil.getDeclaration(element).trim { it <= ' ' } + if (isOverlap) " {" else ";")
} else if (PsiUtil.matchesType(element, "Method", "Function", "FunctionDefinition", "Constructor")) {
val methodTerminator = when (language) {
ComputerLanguage.Java -> " { /* ... */}"
ComputerLanguage.Kotlin -> " { /* ... */}"
ComputerLanguage.Scala -> " { /* ... */}"
else -> ";"
}
processChildren(
element,
self,
isPrior,
isOverlap,
indent + PsiUtil.getDeclaration(element).trim { it <= ' ' } + (if (isOverlap) " {" else methodTerminator))
} else if (PsiUtil.matchesType(element, "LocalVariable")) {
currentContext.children.add(PsiClassContext(indent + text.trim { it <= ' ' } + ";",
isPrior,
isOverlap))
isOverlap,
language))
} else if (PsiUtil.matchesType(element, "Class")) {
processChildren(
element,
Expand All @@ -60,7 +75,7 @@ class PsiClassContext(val text: String, val isPrior: Boolean, val isOverlap: Boo
isOverlap,
indent + text.substring(0, text.indexOf('{')).trim { it <= ' ' } + " {")
if (!isOverlap) {
currentContext.children.add(PsiClassContext("}", isPrior, false))
currentContext.children.add(PsiClassContext("}", isPrior, false, language))
}
} else if (!isOverlap && PsiUtil.matchesType(element, "CodeBlock", "ForStatement")) {
// Skip
Expand All @@ -76,7 +91,7 @@ class PsiClassContext(val text: String, val isPrior: Boolean, val isOverlap: Boo
isOverlap: Boolean,
declarationText: String
): PsiClassContext {
val newNode = PsiClassContext(declarationText, isPrior, isOverlap)
val newNode = PsiClassContext(declarationText, isPrior, isOverlap, language)
currentContext.children.add(newNode)
val prevclassBuffer = currentContext
currentContext = newNode
Expand Down Expand Up @@ -110,12 +125,12 @@ class PsiClassContext(val text: String, val isPrior: Boolean, val isOverlap: Boo
}

companion object {
fun getContext(psiFile: PsiFile, selectionStart: Int, selectionEnd: Int): PsiClassContext {
return PsiClassContext("", false, true).init(psiFile, selectionStart, selectionEnd)
fun getContext(psiFile: PsiFile, selectionStart: Int, selectionEnd: Int, language: ComputerLanguage): PsiClassContext {
return PsiClassContext("", false, true, language).init(psiFile, selectionStart, selectionEnd)
}

fun getContext(psiFile: PsiFile): PsiClassContext {
return getContext(psiFile, 0, psiFile.textLength)
fun getContext(language: ComputerLanguage, psiFile: PsiFile): PsiClassContext {
return getContext(psiFile, 0, psiFile.textLength, language)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class PsiTranslationSkeleton(private val stubId: String?, text: String?, private
.setInputAttribute("language", sourceLanguage.name)
.setOutputType("translated")
.setOutputAttrute("language", targetLanguage.name)
.buildCompletionRequest(), indent!!
.buildCompletionRequest(), indent
)
}
}
Expand Down Expand Up @@ -212,16 +212,16 @@ class PsiTranslationSkeleton(private val stubId: String?, text: String?, private
ComputerLanguage.Python -> String.format(
"%s\n %s\n pass",
declaration,
targetLanguage.lineComment!!.fromString(stubID)
targetLanguage.lineComment.fromString(stubID)
)

ComputerLanguage.Go, ComputerLanguage.Kotlin, ComputerLanguage.Scala, ComputerLanguage.Java, ComputerLanguage.JavaScript, ComputerLanguage.Rust -> String.format(
"%s {\n%s\n}\n",
declaration,
targetLanguage.lineComment!!.fromString(stubID)
targetLanguage.lineComment.fromString(stubID)
)

else -> String.format("%s {\n%s\n}\n", declaration, targetLanguage.lineComment!!.fromString(stubID))
else -> String.format("%s {\n%s\n}\n", declaration, targetLanguage.lineComment.fromString(stubID))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ object PsiUtil {
visitor.set(object : PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
val textRange = element.textRange
if (within(textRange, selectionStart, selectionEnd)) {
if (intersects(TextRange(selectionStart, selectionEnd), textRange)) {
if (matchesType(element, *types)) {
largest.updateAndGet { s: PsiElement? -> if ((s?.text?.length ?: Int.MAX_VALUE) < element.text.length) s else element }
}
Expand All @@ -124,6 +124,7 @@ object PsiUtil {
return largest.get()
}


fun getAllIntersecting(
element: PsiElement,
selectionStart: Int,
Expand All @@ -149,7 +150,11 @@ object PsiUtil {
}

private fun within(textRange: TextRange, vararg offset: Int) : Boolean =
(textRange.startOffset <= offset.maxOrNull()?:0) && (textRange.endOffset > offset.minOrNull()?:0)
offset.filter { it in textRange.startOffset..textRange.endOffset }.isNotEmpty()

private fun intersects(a: TextRange, b: TextRange): Boolean {
return within(a, b.startOffset, b.endOffset) || within(b, a.startOffset, a.endOffset)
}


fun matchesType(element: PsiElement, vararg types: CharSequence): Boolean {
Expand Down
Loading

0 comments on commit 11f5aed

Please sign in to comment.