Skip to content

Commit

Permalink
IJ-CR-131092 the first step in Dart macro support: introduce DartAnal…
Browse files Browse the repository at this point in the history
…ysisServerService.lspMessage_dart_textDocumentContent()

close #903

(cherry picked from commit 1c825c586c0197369497d129a0040c2eeb7b9ddc)

GitOrigin-RevId: ef50e62cad6e50daf55b483088cf79b8c6b1cf8d
  • Loading branch information
jwren authored and intellij-monorepo-bot committed May 15, 2024
1 parent 588ffe7 commit fcb67f7
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public final class DartAnalysisServerService implements Disposable {
private static final long EXECUTION_CREATE_CONTEXT_TIMEOUT = TimeUnit.SECONDS.toMillis(1);
private static final long EXECUTION_MAP_URI_TIMEOUT = TimeUnit.SECONDS.toMillis(1);
private static final long ANALYSIS_IN_TESTS_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
private static final long LSP_MESSAGE_TEXT_DOCUMENT_CONTENT_TIMEOUT = TimeUnit.SECONDS.toMillis(50);
private static final long TESTS_TIMEOUT_COEFF = 10;

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

// LSP over Legacy Dart Analysis Server protocols
public @Nullable String lspMessage_dart_textDocumentContent(@NotNull String uri) {
RemoteAnalysisServerImpl server = myServer;
if (server == null) {
return null;
}

Ref<String> resultRef = new Ref<>();
CountDownLatch latch = new CountDownLatch(1);
server.lspMessage_dart_textDocumentContent(uri, new DartLspTextDocumentContentConsumer() {
@Override
public void computedDocumentContents(String contents) {
resultRef.set(contents);
latch.countDown();
}

@Override
public void onError(RequestError error) {
logError("lspMessage_dart_textDocumentContent()", uri, error);
latch.countDown();
}
});

awaitForLatchCheckingCanceled(server, latch, LSP_MESSAGE_TEXT_DOCUMENT_CONTENT_TIMEOUT);

if (latch.getCount() > 0) {
logTookTooLongMessage("lspMessage_dart_textDocumentContent", LSP_MESSAGE_TEXT_DOCUMENT_CONTENT_TIMEOUT, uri);
}
return resultRef.get();
}

private void startServer(@NotNull final DartSdk sdk) {
if (DartPubActionBase.isInProgress()) return; // DartPubActionBase will start the server itself when finished

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024, the Dart project authors.
*
* Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.eclipse.org/legal/epl-v10.html
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.dart.server;

import org.dartlang.analysis.server.protocol.RequestError;

public interface DartLspTextDocumentContentConsumer extends Consumer {

public void computedDocumentContents(String contents);

/**
* If the file contents can't be sent back, some {@link RequestError} is passed back instead.
*
* @param requestError the reason why a result was not passed back
*/
public void onError(RequestError requestError);
}
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,14 @@ public void received() {
stopServer();
}

//
// LSP over Legacy Dart Analysis Server protocol
//
public void lspMessage_dart_textDocumentContent(String uri, DartLspTextDocumentContentConsumer consumer) {
String id = generateUniqueId();
sendRequestToServer(id, RequestUtilities.generateLSPMessage_dart_textDocumentContent(id, uri), consumer);
}

/**
* Starts the analysis server.
*
Expand Down Expand Up @@ -929,6 +937,12 @@ else if (consumer instanceof BasicConsumer) {
else if (consumer instanceof JsonConsumer) {
((JsonConsumer)consumer).onResponse(resultObject, requestError);
}
//
// LSP over Legacy DAS Dart Analysis Server protocol
//
else if (consumer instanceof DartLspTextDocumentContentConsumer) {
new DartLspTextDocumentContentProcessor((DartLspTextDocumentContentConsumer)consumer).process(resultObject, requestError);
}

synchronized (consumerMapLock) {
consumerMap.remove(idString);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024, the Dart project authors.
*
* Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.eclipse.org/legal/epl-v10.html
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.dart.server.internal.remote.processor;

import com.google.dart.server.DartLspTextDocumentContentConsumer;
import com.google.gson.JsonObject;

import org.dartlang.analysis.server.protocol.RequestError;

/**
* Instances of {@code DartLspTextDocumentContentProcessor} translate JSON result objects for a given
* {@link LSPDartTextDocumentContentConsumer}.
*
* @coverage dart.server.remote
*/
public class DartLspTextDocumentContentProcessor extends ResultProcessor {

private final DartLspTextDocumentContentConsumer consumer;

public DartLspTextDocumentContentProcessor(DartLspTextDocumentContentConsumer consumer) {
this.consumer = consumer;
}

public void process(JsonObject resultObject, RequestError requestError) {
if (resultObject != null) {
// Example: {"lspResponse":{"id":"1","jsonrpc":"2.0","result":{"content":"file contents"}}}
JsonObject lspResponse = resultObject.getAsJsonObject("lspResponse");
JsonObject innerResultObject = lspResponse.getAsJsonObject("result");
final String contents = innerResultObject.get("content").getAsString();
consumer.computedDocumentContents(contents);
}
if (requestError != null) {
consumer.onError(requestError);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,46 @@ private static String getRequestMethod(JsonObject request) {
return null;
}

//
// LSP over Legacy DAS (Dart Analysis Server) protocol below
//
public static final String LSP_DART_TEXT_DOCUMENT_CONTENT = "dart/textDocumentContent";

private static final String LSP_HANDLE = "lsp.handle";

public static final String LSP_JSONRPC = "jsonrpc";

public static final String LSP_JSONROC_VERSION = "2.0";

public static final String LSP_MESSAGE = "lspMessage";

/**
* Generate and return a LSP over Legacy DAS request.
*
* Example:
* {"id":"2","method":"lsp.handle","params":
* {"lspMessage":
* {"id":0,"jsonrpc":"2.0","method":"dart/textDocumentContent","params":
* {"position":{"character":7,"line":1},
* "textDocument":{"uri":"some-uri"}}}}}
*/
private static JsonObject generateLSPMessage(String idValue, String lspMethod, JsonObject lspParams) {
JsonObject lspMessageParams = new JsonObject();
lspMessageParams.addProperty(ID, idValue);
lspMessageParams.addProperty(LSP_JSONRPC, LSP_JSONROC_VERSION);
lspMessageParams.addProperty(METHOD, lspMethod);
lspMessageParams.add(PARAMS, lspParams);
JsonObject lspMessage = new JsonObject();
lspMessage.add(LSP_MESSAGE, lspMessageParams);
return buildJsonObjectRequest(idValue, LSP_HANDLE, lspMessage);
}

public static JsonObject generateLSPMessage_dart_textDocumentContent(String idValue, String uri) {
JsonObject lspParams = new JsonObject();
lspParams.addProperty("uri", uri);
return generateLSPMessage(idValue, LSP_DART_TEXT_DOCUMENT_CONTENT, lspParams);
}

private RequestUtilities() {
}
}

0 comments on commit fcb67f7

Please sign in to comment.