Skip to content

Commit

Permalink
(#151) EditorServicesLanguageHostStarter: warning cleanup, improve th…
Browse files Browse the repository at this point in the history
…reading
  • Loading branch information
ForNeVeR committed Feb 22, 2024
1 parent 7612581 commit 9d24ec7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [Unreleased] (2.3.2)
### Fixed
A bit more of IO work during language server connection moved to the corresponding threads, saving some IDE responsibility in rare cases when it could be a problem.

## [2.3.1] - 2023-12-30
### Fixed
- [#172](https://github.com/ant-druha/intellij-powershell/issues/172): Console output forcibly wraps output to 80 characters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.intellij.openapi.application.ApplicationInfo
import com.intellij.openapi.application.ApplicationNamesInfo
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.rd.util.withSyncIOBackgroundContext
import com.intellij.openapi.util.Key
import com.intellij.openapi.util.SystemInfo
import com.intellij.openapi.util.io.FileUtil
Expand Down Expand Up @@ -139,39 +140,44 @@ open class EditorServicesLanguageHostStarter(protected val myProject: Project) :
if (sessionInfo is SessionInfo.Pipes) {
val readPipeName = sessionInfo.languageServiceReadPipeName
val writePipeName = sessionInfo.languageServiceWritePipeName
return if (SystemInfo.isWindows) {
val readPipe = RandomAccessFile(readPipeName, "rwd")
val writePipe = RandomAccessFile(writePipeName, "r")
val serverReadChannel = readPipe.channel
val serverWriteChannel = writePipe.channel
val inSf = Channels.newInputStream(serverWriteChannel)
val outSf = BufferedOutputStream(Channels.newOutputStream(serverReadChannel))
Pair(inSf, outSf)
} else {
val readSock = AFUNIXSocket.newInstance()
val writeSock = AFUNIXSocket.newInstance()
readSock.connect(AFUNIXSocketAddress(File(readPipeName)))
writeSock.connect(AFUNIXSocketAddress(File(writePipeName)))
Pair(writeSock.inputStream, readSock.outputStream)
return withSyncIOBackgroundContext {
if (SystemInfo.isWindows) {
val readPipe = RandomAccessFile(readPipeName, "rwd")
val writePipe = RandomAccessFile(writePipeName, "r")
val serverReadChannel = readPipe.channel
val serverWriteChannel = writePipe.channel
val inSf = Channels.newInputStream(serverWriteChannel)
val outSf = BufferedOutputStream(Channels.newOutputStream(serverReadChannel))
Pair(inSf, outSf)
} else {
val readSock = AFUNIXSocket.newInstance()
val writeSock = AFUNIXSocket.newInstance()
readSock.connect(AFUNIXSocketAddress(File(readPipeName)))
writeSock.connect(AFUNIXSocketAddress(File(writePipeName)))
Pair(writeSock.inputStream, readSock.outputStream)
}
}
} else {
val port = (sessionInfo as? SessionInfo.Tcp)?.languageServicePort ?: return Pair(null, null)
try {
socket = Socket("127.0.0.1", port)
} catch (e: Exception) {
logger.error("Unable to open connection to language host: $e")
}
if (socket == null) {
logger.error("Unable to create socket: " + toString())
}
if (socket?.isConnected == true) {
logger.info("Connection to language host established: ${socket?.localPort} -> ${socket?.port}")
val inputStream = socket?.getInputStream()
val outputStream = socket?.getOutputStream()
if (inputStream != null && outputStream != null) return Pair(inputStream, outputStream)
return withSyncIOBackgroundContext block@{
val port = (sessionInfo as? SessionInfo.Tcp)?.languageServicePort ?: return@block Pair(null, null)
try {
socket = Socket("127.0.0.1", port)
} catch (e: Exception) {
logger.error("Unable to open connection to language host: $e")
}
if (socket == null) {
logger.error("Unable to create socket: " + toString())
}
if (socket?.isConnected == true) {
logger.info("Connection to language host established: ${socket?.localPort} -> ${socket?.port}")
val inputStream = socket?.getInputStream()
val outputStream = socket?.getOutputStream()
if (inputStream != null && outputStream != null) return@block Pair(inputStream, outputStream)
}

Pair(null, null)
}
}
return Pair(null, null)
}

private fun getSessionCount(): Int {
Expand Down Expand Up @@ -239,12 +245,15 @@ open class EditorServicesLanguageHostStarter(protected val myProject: Project) :
} else ""
val scriptText = "$preamble${escapePath(startupScript)} $args"

val scriptFile = File.createTempFile("start-pses-host", ".ps1")
scriptFile.deleteOnExit()
try {
FileUtil.writeToFile(scriptFile, scriptText)
} catch (e: Exception) {
logger.error("Error writing $scriptFile script file: $e")
val scriptFile = withSyncIOBackgroundContext {
File.createTempFile("start-pses-host", ".ps1").apply {
deleteOnExit()
try {
FileUtil.writeToFile(this, scriptText)
} catch (e: Exception) {
logger.error("Error writing $this script file: $e")
}
}
}

FileUtil.createParentDirs(File(logPath))
Expand Down

0 comments on commit 9d24ec7

Please sign in to comment.