openlawlibrary / pygls

A pythonic generic language server
https://pygls.readthedocs.io/en/latest/
Apache License 2.0
563 stars 102 forks source link

Poor experience with recent "Position" changes #382

Closed RossBencina closed 11 months ago

RossBencina commented 11 months ago

I have just been bitten hard by recent changes and I'd like to offer a critique and suggest some improvements:

The problem that I hit is that up until now I have been using utf16_num_units to convert between python string indexes and client position units (I am doing operational-transform like processing on document changes, so need this low level capability). With recent updates, I get the following error:

DeprecationWarning: 'utf16_num_units' has been deprecated, use 'Position.client_num_units' instead

I think I understand the intent of the message, but here are some of the problems that I encountered:

So, my suggestions are:

DeprecationWarning: 'utf16_num_units' has been deprecated, use 'text_document.position_codec.client_num_units' instead
RossBencina commented 11 months ago

I can prepare a PR if you can give me an indication that it will be accepted.

alcarney commented 11 months ago

Thank you for living on the edge and catching issues like this before they are released!

so a better name might be PositionTranslator or PositionCodec

Would PositionEncoding work as a name, since that it the part of the spec it was introduced to help with? Or is that too close to lsprotocol.types.PositionEncodingKind?

I would understand if you decided against adding the workspace field but really, there needs to be only a single codec for the entire workspace, it doesn't need to be instantiated on a per-text-document basis.

I think putting an instance on the workspace makes sense

I can prepare a PR if you can give me an indication that it will be accepted.

If you want to open a PR that would be fantastic! :)

RossBencina commented 11 months ago

Would PositionEncoding work as a name, since that it the part of the spec it was introduced to help with? Or is that too close to lsprotocol.types.PositionEncodingKind?

The issue there is that lsprotocol.types.ServerCapabilities.position_encoding is a Optional[Union[PositionEncodingKind, str]]

so the position_encoding field name is already used in lsprotocol for storing PositionEncodingKind. If we define a PositionEncoding class, we'd also want to name fields of that type position_encoding which is potentially a little confusing. For example:

encoding_kind: lsprotocol.types.PositionEncodingKind # in our code

and

position_encoding: PositionEncoding # our type

but meanwhile in lsprotocol

lsprotocol.types.ServerCapabilities.position_encoding: Optional[Union[PositionEncodingKind, str]]

The alternative that I'm suggesting is:

encoding_kind: lsprotocol.types.PositionEncodingKind # in our code
position_codec: PositionCodec # our type
lsprotocol.types.ServerCapabilities.position_encoding: Optional[Union[PositionEncodingKind, str]] # in lsprotocol

I'm more than happy for you to make the final call.

alcarney commented 11 months ago

If we define a PositionEncoding class, we'd also want to name fields of that type position_encoding which is potentially a little confusing.

Makes sense, I'll defer to your judgement :)