Racket Language Server
1 Custom Extensions
1.1 Colorize Notification
1.2 Indent Request
2 Application Programming Interface
2.1 Language support
token
eof-token
eof-token?
get-lexer
make-tokenizer
apply-tokenizer-maker
list->producer
lang-tokenizer
skip-white
sexp-comment-reclassifier
token-stream-matcher
semantic-reclassifier
2.2 Protocol
3 References
4 License
6.12

Racket Language Server

David Craven

 (require racket-language-server)
  package: racket-language-server

Implementation of the Language Server Protocol for Racket.

    1 Custom Extensions

      1.1 Colorize Notification

      1.2 Indent Request

    2 Application Programming Interface

      2.1 Language support

      2.2 Protocol

    3 References

    4 License

1 Custom Extensions

1.1 Colorize Notification

Colorize notifications are sent from the server to the client to enable semantic syntax highlighting.

When a file changes it is the server’s responsibility to re-compute syntax highlighting and push them to the client. Newly pushed tokens always replace previously pushed tokens. There is no merging that happens on the client side.

Notification:
  • method: ’racket/colorize’

  • params: ‘RacketColorizeParams‘ defined as follows:

interface RacketColorizeParams {

    /**

     * The URI for which colorization is reported.

     */

    uri: DocumentUri;

 

    /**

     * An array of diagnostic information items.

     */

    tokens: RacketToken[];

}

 

interface RacketToken {

    /**

     * The kind of token.

     */

    kind: RacketTokenKind;

 

    /**

     * The lexer mode used.

     */

    mode: RacketLexerMode;

 

    /**

     * Range of the token.

     */

    range: Range;

}

 

enum RacketLexerMode {

    Racket = "racket",

    Scribble = "scribble",

    Other = "other"

}

 

enum RacketTokenKind {

    // Generic token kinds

    Symbol = "symbol",

    Keyword = "keyword",

    Comment = "comment",

    String = "string",

    Constant = "constant",

    Parenthesis = "parenthesis",

    Error = "error",

    NoColor = "no-color",

    Other = "other",

    // Racket token kinds

    HashColonKeyword = "hash-colon-keyword",

    SexpComment = "sexp-comment",

    // Racket semantic token kinds

    Imported = "imported",

    LexicallyBound = "lexically-bound",

    Free = "free",

    Set!d = "set!d",

    // Scribble token kinds

    Text = "text"

}

1.2 Indent Request

Indent requests are sent from the client to the server to compute the indentation for a line number.

Request:
  • method: ’racket/indent’

  • params: ‘RacketIndentParams‘ defined as follows:

interface RacketIndentParams {

    /**

     * The TextDocument for which indentation is requested.

     */

    textDocument: TextDocumentIdentifier;

 

    /**

     * The line to indent.

     */

    line: number;

}

Response:
  • result: ‘number‘

  • error: code and message set in case an exception happens during the definition request.

2 Application Programming Interface

The codebase is split in two folders, ‘lang‘ which provides Racket language support procedures that extract and combine information from drracket and other sources and ‘protocol‘ which converts the information to the Language Server Protocol.

2.1 Language support

struct

(struct token (lexeme type data start end mode offset)
    #:extra-constructor-name make-token)
  lexeme : any/c
  type : (or/c symbol? false/c)
  data : any/c
  start : (or/c exact-nonnegative-integer? false/c)
  end : (or/c exact-nonnegative-integer? false/c)
  mode : (or/c (one-of/c 'racket 'scribble 'lang 'other) false/c)
  offset : (or/c exact-integer? false/c)
This struct represents a token.
An alias for a token with the lexeme set to eof.

procedure

(eof-token? tok)  boolean?

  tok : any/c
Determines whether a token is an eof-token.

procedure

(get-lexer in)  (-> struct?)

  in : input-port?
Returns a token producer for an input-port.

procedure

(make-tokenizer str)  (-> struct?)

  str : string?
Returns a token producer for an input string.

procedure

(apply-tokenizer-maker tokenizer val)  (listof struct?)

  tokenizer : (-> string? (-> struct?))
  val : string?
Applies a tokenizer to a string and returns a list of tokens.

procedure

(list->producer tokens)  generator?

  tokens : (listof struct?)
Returns a producer for a list of tokens.

procedure

(lang-tokenizer next-token)  (-> struct?)

  next-token : (-> struct?)
Transforms tokens with type 'other that contain a #lang declaration into a 'keyword, 'white-space and 'symbol token.

procedure

(skip-white next-token)  (-> struct?)

  next-token : (-> struct?)
Takes a token producer and returns a new token producer that ignores tokens with type 'white-space and 'whitespace.

procedure

(sexp-comment-reclassifier next-token)  (-> struct?)

  next-token : (-> struct?)
Counts parenthesis and reclassifies tokens after a 'sexp-comment tokens into 'comment tokens.

procedure

(token-stream-matcher old-tokens)

  (-> (-> struct?) (-> struct?))
  old-tokens : (listof struct?)
Matches a token producer to an old list of tokens and sets the offset field.

procedure

(semantic-reclassifier intervals 
  [old-intervals]) 
  (-> (-> struct?) (-> struct?))
  intervals : interval-map?
  old-intervals : boolean? = #f
Set’s data field in a token using an interval-map. If old-intervals is #t it will use the token’s offset for lookup.

2.2 Protocol

3 References

4 License

Copyright 2018 David Craven and others

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.