Skip to content

Conversation

@Veetaha
Copy link
Contributor

@Veetaha Veetaha commented Feb 24, 2020

More type-safety to the god of type-safety.

* This file mirrors `crates/rust-analyzer/src/req.rs` declarations.
*/

import { RequestType, TextDocumentIdentifier, Position, Range, TextDocumentPositionParams, Location, NotificationType, WorkspaceEdit } from "vscode-languageclient";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import * as lc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followed the same import pattern we have in req.rs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, done

@@ -0,0 +1,117 @@
/**
* This file mirrors `crates/rust-analyzer/src/req.rs` declarations.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's unfortunate that the files are named differently, but I also actively dislike both names :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an aside I really dislike req.rs... it should probably be custom_requests.rs or something with only the imports required to support that file. We end up importing a lot from that file in handlers and conv.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is awesome, unless come up with better!

tokenSource.token
)
return sendRequestWithRetry(this.ctx.client, ra.inlayHints, request, tokenSource.token)
.catch(_ => null)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well swtich to client.logFailedRequest here and elsewhere?

Copy link
Contributor Author

@Veetaha Veetaha Feb 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Repeating client.logFailedRequest everywhere would be very annoying. What if we add the function that wraps sendRequest and logs on failure or wrap the client with our own RaClient?

class RustAnalyzerClient {
    inner: lc.LanguageClient;

    async sendRequest(...params) {
        return this.inner.sendRequest(...params).catch(err => {
            this.inner.logFailedRequest(...);
            throw err;
        });
    }

    async sendRequestWithRetry(...params) { ... }
  
    get p2c() { return this.inner.protocol2CodeConverter; }
    get c2p() { return this.inner.code2ProtocolConverter; }
    /* stuff */ 
}

}

interface InlayHint {
range: vscode.Range;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SomeoneToIgnore what was the reason to declare vscode.Range instead of lc.Range here? It worked all the time since their shape is structurally the same, but we may not rely on this, so I explicitly added protocol2CodeConverter.asRange() call here...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No particular reason, pick the one that suits better and works still.


type Option<T> = null | T;
type Vec<T> = T[];
type FxHashMap<K extends PropertyKey, V> = Record<K, V>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheatsheet for Rust -> TS transformations...

(_error: any) => {
// FIXME: switch to the more modern (?) typed request infrastructure
// client.logFailedRequest(OnEnterRequest.type, error);
return Promise.resolve(null);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: @matklad there is no need to wrap the return value into a promise in the then(onfulfilled, onrejected)/catch(onrejected) handlers.

if (!editor || editor.document.languageId !== 'rust') return;
if (!ctx.config.highlightingOn) return;
const client = ctx.client;
if (!client) return;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should get rid of these if statements, since the client is not nullable today...
But this deserves a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, there's FIXME about this in main.ts

Copy link
Contributor

@kjeremy kjeremy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

@matklad
Copy link
Contributor

matklad commented Feb 25, 2020

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 25, 2020

@bors bors bot merged commit 5e6f4ca into rust-lang:master Feb 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants