Skip to content

Commit fcb67f7

Browse files
jwrenintellij-monorepo-bot
authored andcommitted
IJ-CR-131092 the first step in Dart macro support: introduce DartAnalysisServerService.lspMessage_dart_textDocumentContent()
close #903 (cherry picked from commit 1c825c586c0197369497d129a0040c2eeb7b9ddc) GitOrigin-RevId: ef50e62cad6e50daf55b483088cf79b8c6b1cf8d
1 parent 588ffe7 commit fcb67f7

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

Dart/src/com/jetbrains/lang/dart/analyzer/DartAnalysisServerService.java

+32
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public final class DartAnalysisServerService implements Disposable {
116116
private static final long EXECUTION_CREATE_CONTEXT_TIMEOUT = TimeUnit.SECONDS.toMillis(1);
117117
private static final long EXECUTION_MAP_URI_TIMEOUT = TimeUnit.SECONDS.toMillis(1);
118118
private static final long ANALYSIS_IN_TESTS_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
119+
private static final long LSP_MESSAGE_TEXT_DOCUMENT_CONTENT_TIMEOUT = TimeUnit.SECONDS.toMillis(50);
119120
private static final long TESTS_TIMEOUT_COEFF = 10;
120121

121122
private static final Logger LOG = Logger.getInstance(DartAnalysisServerService.class);
@@ -2034,6 +2035,37 @@ public void onError(final RequestError error) {
20342035
return resultRef.get();
20352036
}
20362037

2038+
// LSP over Legacy Dart Analysis Server protocols
2039+
public @Nullable String lspMessage_dart_textDocumentContent(@NotNull String uri) {
2040+
RemoteAnalysisServerImpl server = myServer;
2041+
if (server == null) {
2042+
return null;
2043+
}
2044+
2045+
Ref<String> resultRef = new Ref<>();
2046+
CountDownLatch latch = new CountDownLatch(1);
2047+
server.lspMessage_dart_textDocumentContent(uri, new DartLspTextDocumentContentConsumer() {
2048+
@Override
2049+
public void computedDocumentContents(String contents) {
2050+
resultRef.set(contents);
2051+
latch.countDown();
2052+
}
2053+
2054+
@Override
2055+
public void onError(RequestError error) {
2056+
logError("lspMessage_dart_textDocumentContent()", uri, error);
2057+
latch.countDown();
2058+
}
2059+
});
2060+
2061+
awaitForLatchCheckingCanceled(server, latch, LSP_MESSAGE_TEXT_DOCUMENT_CONTENT_TIMEOUT);
2062+
2063+
if (latch.getCount() > 0) {
2064+
logTookTooLongMessage("lspMessage_dart_textDocumentContent", LSP_MESSAGE_TEXT_DOCUMENT_CONTENT_TIMEOUT, uri);
2065+
}
2066+
return resultRef.get();
2067+
}
2068+
20372069
private void startServer(@NotNull final DartSdk sdk) {
20382070
if (DartPubActionBase.isInProgress()) return; // DartPubActionBase will start the server itself when finished
20392071

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2024, the Dart project authors.
3+
*
4+
* Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.eclipse.org/legal/epl-v10.html
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.google.dart.server;
15+
16+
import org.dartlang.analysis.server.protocol.RequestError;
17+
18+
public interface DartLspTextDocumentContentConsumer extends Consumer {
19+
20+
public void computedDocumentContents(String contents);
21+
22+
/**
23+
* If the file contents can't be sent back, some {@link RequestError} is passed back instead.
24+
*
25+
* @param requestError the reason why a result was not passed back
26+
*/
27+
public void onError(RequestError requestError);
28+
}

Dart/thirdPartySrc/analysisServer/com/google/dart/server/internal/remote/RemoteAnalysisServerImpl.java

+14
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,14 @@ public void received() {
606606
stopServer();
607607
}
608608

609+
//
610+
// LSP over Legacy Dart Analysis Server protocol
611+
//
612+
public void lspMessage_dart_textDocumentContent(String uri, DartLspTextDocumentContentConsumer consumer) {
613+
String id = generateUniqueId();
614+
sendRequestToServer(id, RequestUtilities.generateLSPMessage_dart_textDocumentContent(id, uri), consumer);
615+
}
616+
609617
/**
610618
* Starts the analysis server.
611619
*
@@ -929,6 +937,12 @@ else if (consumer instanceof BasicConsumer) {
929937
else if (consumer instanceof JsonConsumer) {
930938
((JsonConsumer)consumer).onResponse(resultObject, requestError);
931939
}
940+
//
941+
// LSP over Legacy DAS Dart Analysis Server protocol
942+
//
943+
else if (consumer instanceof DartLspTextDocumentContentConsumer) {
944+
new DartLspTextDocumentContentProcessor((DartLspTextDocumentContentConsumer)consumer).process(resultObject, requestError);
945+
}
932946

933947
synchronized (consumerMapLock) {
934948
consumerMap.remove(idString);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2024, the Dart project authors.
3+
*
4+
* Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.eclipse.org/legal/epl-v10.html
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package com.google.dart.server.internal.remote.processor;
15+
16+
import com.google.dart.server.DartLspTextDocumentContentConsumer;
17+
import com.google.gson.JsonObject;
18+
19+
import org.dartlang.analysis.server.protocol.RequestError;
20+
21+
/**
22+
* Instances of {@code DartLspTextDocumentContentProcessor} translate JSON result objects for a given
23+
* {@link LSPDartTextDocumentContentConsumer}.
24+
*
25+
* @coverage dart.server.remote
26+
*/
27+
public class DartLspTextDocumentContentProcessor extends ResultProcessor {
28+
29+
private final DartLspTextDocumentContentConsumer consumer;
30+
31+
public DartLspTextDocumentContentProcessor(DartLspTextDocumentContentConsumer consumer) {
32+
this.consumer = consumer;
33+
}
34+
35+
public void process(JsonObject resultObject, RequestError requestError) {
36+
if (resultObject != null) {
37+
// Example: {"lspResponse":{"id":"1","jsonrpc":"2.0","result":{"content":"file contents"}}}
38+
JsonObject lspResponse = resultObject.getAsJsonObject("lspResponse");
39+
JsonObject innerResultObject = lspResponse.getAsJsonObject("result");
40+
final String contents = innerResultObject.get("content").getAsString();
41+
consumer.computedDocumentContents(contents);
42+
}
43+
if (requestError != null) {
44+
consumer.onError(requestError);
45+
}
46+
}
47+
}

Dart/thirdPartySrc/analysisServer/com/google/dart/server/internal/remote/utilities/RequestUtilities.java

+40
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,46 @@ private static String getRequestMethod(JsonObject request) {
11991199
return null;
12001200
}
12011201

1202+
//
1203+
// LSP over Legacy DAS (Dart Analysis Server) protocol below
1204+
//
1205+
public static final String LSP_DART_TEXT_DOCUMENT_CONTENT = "dart/textDocumentContent";
1206+
1207+
private static final String LSP_HANDLE = "lsp.handle";
1208+
1209+
public static final String LSP_JSONRPC = "jsonrpc";
1210+
1211+
public static final String LSP_JSONROC_VERSION = "2.0";
1212+
1213+
public static final String LSP_MESSAGE = "lspMessage";
1214+
1215+
/**
1216+
* Generate and return a LSP over Legacy DAS request.
1217+
*
1218+
* Example:
1219+
* {"id":"2","method":"lsp.handle","params":
1220+
* {"lspMessage":
1221+
* {"id":0,"jsonrpc":"2.0","method":"dart/textDocumentContent","params":
1222+
* {"position":{"character":7,"line":1},
1223+
* "textDocument":{"uri":"some-uri"}}}}}
1224+
*/
1225+
private static JsonObject generateLSPMessage(String idValue, String lspMethod, JsonObject lspParams) {
1226+
JsonObject lspMessageParams = new JsonObject();
1227+
lspMessageParams.addProperty(ID, idValue);
1228+
lspMessageParams.addProperty(LSP_JSONRPC, LSP_JSONROC_VERSION);
1229+
lspMessageParams.addProperty(METHOD, lspMethod);
1230+
lspMessageParams.add(PARAMS, lspParams);
1231+
JsonObject lspMessage = new JsonObject();
1232+
lspMessage.add(LSP_MESSAGE, lspMessageParams);
1233+
return buildJsonObjectRequest(idValue, LSP_HANDLE, lspMessage);
1234+
}
1235+
1236+
public static JsonObject generateLSPMessage_dart_textDocumentContent(String idValue, String uri) {
1237+
JsonObject lspParams = new JsonObject();
1238+
lspParams.addProperty("uri", uri);
1239+
return generateLSPMessage(idValue, LSP_DART_TEXT_DOCUMENT_CONTENT, lspParams);
1240+
}
1241+
12021242
private RequestUtilities() {
12031243
}
12041244
}

0 commit comments

Comments
 (0)