Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,5 @@ Just open the project and use “Run Plugin” in the run drop down.
## Develop with Neovim

- Make sure to enable [`kotlin_language_server` from nvim-lspconfig][nvim-kls].
- Overwrite `cmd` by following [this fix][kls-fix] ([use the new Kotlin language server][community-kls]
because this language server is better maintained works with this plugin)

[nvim-kls]: https://github.com/neovim/nvim-lspconfig/blob/master/doc/configs.md#kotlin_language_server
[kls-fix]: https://github.com/fwcd/kotlin-language-server/issues/600#issuecomment-2471327399
[community-kls]: https://github.com/kotlin-community-tools/kotlin-language-server
2 changes: 1 addition & 1 deletion src/main/kotlin/io/github/ethersync/EthersyncService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.github.ethersync

interface EthersyncService {

fun start(peer: String?)
fun start(joinCode: String?)

fun shutdown()

Expand Down
54 changes: 15 additions & 39 deletions src/main/kotlin/io/github/ethersync/EthersyncServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import org.eclipse.lsp4j.jsonrpc.ResponseErrorException
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import java.nio.file.Files
import java.nio.file.attribute.PosixFilePermissions
import java.util.concurrent.Executors

private val LOG = logger<EthersyncServiceImpl>()
Expand Down Expand Up @@ -128,20 +130,16 @@ class EthersyncServiceImpl(
cursortracker.clear()
}

override fun start(peer: String?) {
val socket = "ethersync-%s-socket".format(project.name)

override fun start(joinCode: String?) {
val cmd = GeneralCommandLine(AppSettings.getInstance().state.ethersyncBinaryPath)

cmd.addParameter("daemon")
peer?.let {
if (peer.isNotBlank()) {
cmd.addParameter("--peer")
cmd.addParameter(peer)
}
if (joinCode == null || joinCode.trim().isEmpty()) {
cmd.addParameter("share")
}
else {
cmd.addParameter("join")
cmd.addParameter(joinCode.trim())
}
cmd.addParameter("--socket-name")
cmd.addParameter(socket)

launchDaemon(cmd)
}
Expand All @@ -158,33 +156,22 @@ class EthersyncServiceImpl(
val ethersyncDirectory = File(projectDirectory, ".ethersync")
cmd.workDirectory = projectDirectory

var socket: String? = null
if (cmd.parametersList.hasParameter("--socket-name") || cmd.parametersList.hasParameter("-s")) {
for (i in 0..(cmd.parametersList.parametersCount - 1)) {
val name = cmd.parametersList[i]

if (name == "--socket-name" || name == "-s") {
socket = cmd.parametersList[i + 1]
break
}
}
}

cs.launch {
shutdownImpl()

if (!ethersyncDirectory.exists()) {
LOG.debug("Creating ethersync directory")
ethersyncDirectory.mkdir()
val permissions = PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------"));
Files.createDirectory(ethersyncDirectory.toPath(), permissions);
}

withContext(Dispatchers.EDT) {
daemonProcess = ColoredProcessHandler(cmd)

daemonProcess!!.addProcessListener(object : ProcessListener {
override fun onTextAvailable(event: ProcessEvent, outputType: Key<*>) {
if (event.text.contains("Others can connect with")) {
launchEthersyncClient(socket, projectDirectory)
if (event.text.contains("One other person can use this to connect to you") || event.text.contains("Connected to peer:")) {
launchEthersyncClient(projectDirectory)
}
}

Expand Down Expand Up @@ -221,26 +208,15 @@ class EthersyncServiceImpl(
}
}

private fun launchEthersyncClient(socket: String?, projectDirectory: File) {
private fun launchEthersyncClient(projectDirectory: File) {
if (clientProcess != null) {
return
}

cs.launch {
val cmd = GeneralCommandLine(AppSettings.getInstance().state.ethersyncBinaryPath)
cmd.workDirectory = projectDirectory
cmd.addParameter("client")

socket?.let {
cmd.addParameter("--socket-name")
cmd.addParameter(it)
}

LOG.info("Starting ethersync client")
// TODO: try catch not existing binary
val clientProcessBuilder = ProcessBuilder(
AppSettings.getInstance().state.ethersyncBinaryPath,
"client", "--socket-name", socket)
val clientProcessBuilder = ProcessBuilder(AppSettings.getInstance().state.ethersyncBinaryPath, "client")
.directory(projectDirectory)
clientProcess = clientProcessBuilder.start()
val clientProcess = clientProcess!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ class StartEthersyncDaemonAction : AnAction("Connect to peer", "Connect to a run

override fun actionPerformed(e: AnActionEvent) {
val project = e.project ?: return
val address = Messages.showInputDialog(
val joinCode = Messages.showInputDialog(
project,
"Provide ethersync peer address. Leave empty if you want to host a new session.",
"Peer Address",
"Provide ethersync join code. Leave empty if you want to host a new session.",
"Join Code",
Icons.ToolbarIcon
)

if (address != null) {
if (joinCode != null) {
val service = project.service<EthersyncService>()

service.start(address)
service.start(joinCode)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ class StartEthersyncDaemonTerminalShellCommandHandler : TerminalShellCommandHand
command: String,
executor: Executor
): Boolean {
workingDirectory?.let {
if (project.basePath != it) {
return false
}
}
val ethersyncService = project.service<EthersyncService>()
ethersyncService.startWithCustomCommandLine(command)
return true
}

override fun matches(project: Project, workingDirectory: String?, localSession: Boolean, command: String): Boolean {
if (workingDirectory == null) {
return false
}
else if (project.basePath != workingDirectory) {
return false
}

val ethersyncBinary = AppSettings.getInstance().state.ethersyncBinaryPath

if (!command.startsWith(ethersyncBinary)) {
Expand All @@ -33,6 +35,6 @@ class StartEthersyncDaemonTerminalShellCommandHandler : TerminalShellCommandHand

val rest = command.substring(ethersyncBinary.length).trim()

return rest.startsWith("daemon")
return rest.startsWith("share") || rest.startsWith("join")
}
}