|
| 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