Skip to content

Commit

Permalink
Add completion from buildwrapper. (temporary disabled)
Browse files Browse the repository at this point in the history
  • Loading branch information
atsky committed May 23, 2014
1 parent 0be8c6a commit d1eef33
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import java.util.HashSet
import org.jetbrains.haskell.external.GHC_MOD
import org.jetbrains.haskell.psi.ModuleName
import org.jetbrains.haskell.parser.token.KEYWORDS
import org.jetbrains.haskell.external.BuildWrapper
import org.jetbrains.haskell.util.getRelativePath


public class HaskellCompletionContributor() : CompletionContributor() {
Expand All @@ -20,6 +22,20 @@ public class HaskellCompletionContributor() : CompletionContributor() {
if (parameters!!.getCompletionType() == CompletionType.BASIC) {
val psiElement = parameters.getPosition()

/*
val moduleContent = BuildWrapper.getModuleContentDir(psiElement)
val path = getRelativePath(moduleContent.getPath(),
parameters.getOriginalFile().getVirtualFile()!!.getPath())
val names = BuildWrapper.init(moduleContent).namesinscope(path)
if (names != null) {
for (value in names) {
val text = value as String
val indexOf = text.lastIndexOf(".")
result!!.addElement(LookupElementBuilder.create(text.substring(indexOf + 1))!!)
}
}
*/

for (value in KEYWORDS) {
result!!.addElement(LookupElementBuilder.create(value.myName)!!)
}
Expand Down
29 changes: 29 additions & 0 deletions plugin/src/org/jetbrains/haskell/external/BuildWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,24 @@ import org.json.simple.JSONValue
import org.json.simple.JSONArray
import org.jetbrains.haskell.config.HaskellSettings
import java.io.IOException
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiFile
import com.intellij.openapi.module.ModuleUtilCore
import com.intellij.psi.PsiElement

/**
* Created by atsky on 12/05/14.
*/
class BuildWrapper(val path : String,
val cabalFile : String) {
class object {
public fun init(moduleContent : VirtualFile) : BuildWrapper {
val cabals = moduleContent.getChildren()!!.filter { it.getName().endsWith(".cabal") }
val cabal = cabals.head!!.getPath()

return BuildWrapper(moduleContent.getPath(), cabal)
}

fun getProbrammPath(): String {
return HaskellSettings.getInstance().getState().buildWrapperPath!!
}
Expand All @@ -28,6 +39,24 @@ class BuildWrapper(val path : String,
}

}

fun getModuleContentDir(file: PsiElement): VirtualFile {
val module = ModuleUtilCore.findModuleForPsiElement(file)
return module!!.getModuleFile()!!.getParent()!!
}
}

fun namesinscope(file : String): JSONArray? {
val out = ProcessRunner(path).execute(
getProbrammPath(), "namesinscope", "-t", ".buildwrapper", "--cabalfile=" + cabalFile, "-f", file)
val prefix = "\nbuild-wrapper-json:"
if (out.startsWith(prefix)) {
val jsonText = out.substring(prefix.size)
val array = JSONValue.parse(jsonText) as JSONArray

return array[0] as JSONArray?
}
return null;
}

fun synchronize() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,14 @@ import com.intellij.openapi.vfs.LocalFileSystem
import java.util.HashSet
import sun.nio.cs.StandardCharsets
import java.io.ByteArrayInputStream
import org.jetbrains.haskell.util.getRelativePath

public class HaskellExternalAnnotator() : ExternalAnnotator<PsiFile, List<ErrorMessage>>() {

override fun collectInformation(file: PsiFile): PsiFile {
return file
}

public fun getRelativePath(base: String, path: String): String {
val bpath = File(base).getCanonicalPath()
val fpath = File(path).getCanonicalPath()

if (fpath.startsWith(bpath)) {
return fpath.substring(bpath.length() + 1)
} else {
throw RuntimeException("Base path " + base + "is wrong to " + path);
}
}

fun copyContent(basePath: VirtualFile, destination: File) {
if (!destination.exists()) {
destination.mkdir()
Expand Down Expand Up @@ -84,25 +74,17 @@ public class HaskellExternalAnnotator() : ExternalAnnotator<PsiFile, List<ErrorM
}
}

fun getModuleContentDir(file: PsiFile): VirtualFile {
val module = ModuleUtilCore.findModuleForPsiElement(file)
return module!!.getModuleFile()!!.getParent()!!
}

override fun doAnnotate(psiFile: PsiFile?): List<ErrorMessage> {
val file = psiFile!!.getVirtualFile()
if (file == null) {
return listOf()
}

val moduleContent = getModuleContentDir(psiFile)
val moduleContent = BuildWrapper.getModuleContentDir(psiFile)

copyContent(moduleContent, File(moduleContent.getCanonicalPath()!!, ".buildwrapper"))

val cabals = moduleContent.getChildren()!!.filter { it.getName().endsWith(".cabal") }
val cabal = cabals.head!!.getPath()

val buildWrapper = BuildWrapper(moduleContent.getPath(), cabal)
val buildWrapper = BuildWrapper.init(moduleContent)

val path = getRelativePath(moduleContent.getPath(), file.getPath())
val out = buildWrapper.build1(path)
Expand All @@ -118,7 +100,7 @@ public class HaskellExternalAnnotator() : ExternalAnnotator<PsiFile, List<ErrorM


override fun apply(file: PsiFile, annotationResult: List<ErrorMessage>?, holder: AnnotationHolder) {
val moduleContent = getModuleContentDir(file)
val moduleContent = BuildWrapper.getModuleContentDir(file)
val relativePath = getRelativePath(moduleContent.getPath(), file.getVirtualFile()!!.getPath())

for (error in annotationResult!!) {
Expand Down
1 change: 1 addition & 0 deletions plugin/src/org/jetbrains/haskell/util/FileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public fun joinPath(first : String, vararg more : String) : String {
return result
}


public fun copyFile(iStream : InputStream, destination: File) {
val oStream = FileOutputStream(destination);
try {
Expand Down

0 comments on commit d1eef33

Please sign in to comment.