Skip to content

Commit b8c3a47

Browse files
committed
Move service code to new class
1 parent 2e88d6b commit b8c3a47

File tree

2 files changed

+68
-49
lines changed

2 files changed

+68
-49
lines changed

third_party/src/main/java/com/jetbrains/lang/dart/ide/toolingDaemon/DartToolingDaemonService.kt

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -109,55 +109,7 @@ class DartToolingDaemonService private constructor(val project: Project, cs: Cor
109109
connectToDtdWebSocket(it)
110110
DartAnalysisServerService.getInstance(project).connectToDtd(uri)
111111
}
112-
setUpEditorService()
113-
}
114-
115-
private fun setUpEditorService() {
116-
registerServiceMethod("Editor", "getActiveLocation", JsonObject()) {
117-
val result = ReadAction.nonBlocking<JsonObject> {
118-
getActiveLocation(project, this)
119-
}.executeSynchronously()
120-
result.addProperty("type", "ActiveLocation")
121-
122-
DartToolingDaemonResponse(result, null)
123-
}
124-
125-
registerServiceMethod("Editor", "navigateToCode", JsonObject()) handler@{ request ->
126-
val fileUri: String? = request.get("uri").asString
127-
if (fileUri == null) {
128-
val params = JsonObject()
129-
params.addProperty("message", "No uri provided")
130-
return@handler DartToolingDaemonResponse(null, params)
131-
}
132-
133-
var path: String? = null
134-
try {
135-
path = URI(fileUri).toURL().file
136-
} catch (e: MalformedURLException) {
137-
// A null path will cause an early return.
138-
} catch (e: URISyntaxException) {
139-
}
140-
if (path == null) {
141-
val params = JsonObject()
142-
params.addProperty("message", "Path could not be found from fileUri: $fileUri")
143-
return@handler DartToolingDaemonResponse(null, params)
144-
}
145-
146-
val file = LocalFileSystem.getInstance().findFileByPath(path)
147-
val line: Int = request.get("line").asInt
148-
val column: Int = request.get("column").asInt
149-
150-
ApplicationManager.getApplication().invokeLater(Runnable {
151-
if (file != null && line >= 0 && column >= 0) {
152-
val position = XSourcePositionImpl.create(file, line - 1, column - 1)
153-
position.createNavigatable(project).navigate(false)
154-
}
155-
})
156-
157-
val params = JsonObject()
158-
params.addProperty("success", true)
159-
DartToolingDaemonResponse(params, null)
160-
}
112+
DtdEditorService(project, this).setUpService()
161113
}
162114

163115
private fun connectToDtdWebSocket(uri: String) {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
2+
package com.jetbrains.lang.dart.ide.toolingDaemon
3+
4+
import com.google.gson.JsonObject
5+
import com.intellij.openapi.application.ApplicationManager
6+
import com.intellij.openapi.application.ReadAction
7+
import com.intellij.openapi.diagnostic.logger
8+
import com.intellij.openapi.project.Project
9+
import com.intellij.openapi.vfs.LocalFileSystem
10+
import com.intellij.xdebugger.impl.XSourcePositionImpl
11+
import java.net.MalformedURLException
12+
import java.net.URI
13+
import java.net.URISyntaxException
14+
15+
internal class DtdEditorService(private val project: Project, private val dtdService: DartToolingDaemonService) {
16+
fun setUpService() {
17+
dtdService.registerServiceMethod("Editor", "getActiveLocation", JsonObject()) {
18+
val result = ReadAction.nonBlocking<JsonObject> {
19+
getActiveLocation(project, dtdService)
20+
}.executeSynchronously()
21+
result.addProperty("type", "ActiveLocation")
22+
23+
DartToolingDaemonResponse(result, null)
24+
}
25+
26+
dtdService.registerServiceMethod("Editor", "navigateToCode", JsonObject()) handler@{ request ->
27+
val fileUri: String? = request.get("uri").asString
28+
if (fileUri == null) {
29+
val params = JsonObject()
30+
params.addProperty("message", "No uri provided")
31+
return@handler DartToolingDaemonResponse(null, params)
32+
}
33+
34+
var path: String? = null
35+
try {
36+
path = URI(fileUri).toURL().file
37+
} catch (e: MalformedURLException) {
38+
// A null path will cause an early return.
39+
} catch (e: URISyntaxException) {
40+
}
41+
if (path == null) {
42+
val params = JsonObject()
43+
params.addProperty("message", "Path could not be found from fileUri: $fileUri")
44+
return@handler DartToolingDaemonResponse(null, params)
45+
}
46+
47+
val file = LocalFileSystem.getInstance().findFileByPath(path)
48+
val line: Int = request.get("line").asInt
49+
val column: Int = request.get("column").asInt
50+
51+
ApplicationManager.getApplication().invokeLater(Runnable {
52+
if (file != null && line >= 0 && column >= 0) {
53+
val position = XSourcePositionImpl.create(file, line - 1, column - 1)
54+
position.createNavigatable(project).navigate(false)
55+
}
56+
})
57+
58+
val params = JsonObject()
59+
params.addProperty("success", true)
60+
DartToolingDaemonResponse(params, null)
61+
}
62+
}
63+
64+
companion object {
65+
private val logger = logger<DtdEditorService>()
66+
}
67+
}

0 commit comments

Comments
 (0)