-
Notifications
You must be signed in to change notification settings - Fork 6
Update analyze tool to use LSP, simplify tool #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f6c4e5e
checkpoint, spawning server ourselves
jakemac53 e197201
get analysis working via LSP
jakemac53 36ba99d
Merge branch 'main' into lsp
jakemac53 ea0d4bf
revert json_rpc_2 constraint
jakemac53 492b0a1
drop analyzer dep
jakemac53 5667e76
code review
jakemac53 4b0b0ca
Merge branch 'main' into lsp
jakemac53 a38d599
wait for Editor.getDebugSessions to be registered before returning th…
jakemac53 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
pkgs/dart_tooling_mcp_server/lib/src/lsp/wire_format.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:async/async.dart'; | ||
import 'package:stream_channel/stream_channel.dart'; | ||
|
||
/// Handles LSP communication with its associated headers. | ||
StreamChannel<String> lspChannel( | ||
Stream<List<int>> stream, | ||
StreamSink<List<int>> sink, | ||
) { | ||
final parser = _Parser(stream); | ||
final outSink = StreamSinkTransformer.fromHandlers( | ||
handleData: _serialize, | ||
handleDone: (sink) { | ||
sink.close(); | ||
parser.close(); | ||
}, | ||
).bind(sink); | ||
return StreamChannel.withGuarantees(parser.stream, outSink); | ||
} | ||
|
||
/// Writes [data] to [sink], with the appropriate content length header. | ||
/// | ||
/// Writes the [data] in 1KB chunks. | ||
void _serialize(String data, EventSink<List<int>> sink) { | ||
final message = utf8.encode(data); | ||
final header = 'Content-Length: ${message.length}\r\n\r\n'; | ||
sink.add(ascii.encode(header)); | ||
for (var chunk in _chunks(message, 1024)) { | ||
sink.add(chunk); | ||
} | ||
} | ||
|
||
/// Parses content headers and the following messages. | ||
/// | ||
/// Returns a [stream] of just the message contents. | ||
class _Parser { | ||
/// Controller that gets a single [String] per entire | ||
/// JSON message received as input. | ||
final _messageController = StreamController<String>(); | ||
|
||
/// Stream of full JSON messages in [String] form. | ||
Stream<String> get stream => _messageController.stream; | ||
|
||
/// All the input bytes for the message or header we are currently working | ||
/// with. | ||
final _buffer = <int>[]; | ||
|
||
/// Whether or not we are still parsing the header. | ||
bool _headerMode = true; | ||
|
||
/// The parsed content length, or -1. | ||
int _contentLength = -1; | ||
|
||
/// The subscription for the input bytes stream. | ||
late final StreamSubscription _subscription; | ||
|
||
_Parser(Stream<List<int>> stream) { | ||
_subscription = stream | ||
.expand((bytes) => bytes) | ||
.listen(_handleByte, onDone: _messageController.close); | ||
} | ||
|
||
/// Shut down this parser. | ||
Future<void> close() => _subscription.cancel(); | ||
|
||
/// Handles each incoming byte one at a time. | ||
void _handleByte(int byte) { | ||
_buffer.add(byte); | ||
if (_headerMode && _headerComplete) { | ||
_contentLength = _parseContentLength(); | ||
_buffer.clear(); | ||
_headerMode = false; | ||
} else if (!_headerMode && _messageComplete) { | ||
_messageController.add(utf8.decode(_buffer)); | ||
_buffer.clear(); | ||
_headerMode = true; | ||
} | ||
} | ||
|
||
/// Whether the entire message is in [_buffer]. | ||
bool get _messageComplete => _buffer.length >= _contentLength; | ||
|
||
/// Decodes [_buffer] into a String and looks for the 'Content-Length' header. | ||
int _parseContentLength() { | ||
final asString = ascii.decode(_buffer); | ||
final headers = asString.split('\r\n'); | ||
final lengthHeader = headers.firstWhere( | ||
(h) => h.startsWith('Content-Length'), | ||
); | ||
final length = lengthHeader.split(':').last.trim(); | ||
return int.parse(length); | ||
} | ||
|
||
/// Whether [_buffer] ends in '\r\n\r\n'. | ||
bool get _headerComplete { | ||
final l = _buffer.length; | ||
return l > 4 && | ||
_buffer[l - 1] == 10 && | ||
_buffer[l - 2] == 13 && | ||
_buffer[l - 3] == 10 && | ||
_buffer[l - 4] == 13; | ||
} | ||
} | ||
|
||
/// Splits [data] into chunks of at most [chunkSize]. | ||
Iterable<List<T>> _chunks<T>(List<T> data, int chunkSize) sync* { | ||
if (data.length <= chunkSize) { | ||
yield data; | ||
return; | ||
} | ||
var low = 0; | ||
while (low < data.length) { | ||
if (data.length > low + chunkSize) { | ||
yield data.sublist(low, low + chunkSize); | ||
} else { | ||
yield data.sublist(low); | ||
} | ||
low += chunkSize; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing that the analysis server must already have some similar code. At some point we should look at sharing the implementation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I would probably move it to be a part of the
language_server_protocol
package.