Skip to content

Commit

Permalink
[protocol] New request coq/getDocument to get serialized document.
Browse files Browse the repository at this point in the history
  • Loading branch information
ejgallego committed Feb 16, 2023
1 parent 157f6b6 commit 1c8535e
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
fixes #321)
- Trigger completion on quote char "'" (@ejgallego, #350)
- Fix typo on keybinding config for show goals (@tomtomjhj, #357)
- New request `coq/getDocument` to get serialized full document
contents. Thanks to Clément Pit-Claudel for feedback and ideas.
(@ejgallego, #350)

# coq-lsp 0.1.5: Form
---------------------
Expand Down
3 changes: 3 additions & 0 deletions controller/lsp_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ let do_document_request ~params ~handler =
Postpone (PendingRequest.DocRequest { uri; handler })

let do_symbols = do_document_request ~handler:Rq_symbols.symbols
let do_document = do_document_request ~handler:Rq_document.request

let do_trace params =
let trace = string_field "value" params in
Expand Down Expand Up @@ -336,6 +337,8 @@ let dispatch_request ~method_ ~params : RAction.t =
| "textDocument/hover" -> do_hover ~params
(* Proof-specific stuff *)
| "proof/goals" -> do_goals ~params
(* Coq specific stuff *)
| "coq/getDocument" -> do_document ~params
(* Generic handler *)
| msg ->
LIO.trace "no_handler" msg;
Expand Down
22 changes: 22 additions & 0 deletions controller/rq_document.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(************************************************************************)
(* Coq Language Server Protocol -- Document *)
(* Copyright 2019 MINES ParisTech -- Dual License LGPL 2.1 / GPL3+ *)
(* Copyright 2019-2023 Inria -- Dual License LGPL 2.1 / GPL3+ *)
(* Written by: Emilio J. Gallego Arias *)
(************************************************************************)

let to_span { Fleche.Doc.Node.range; ast; _ } =
let span = Option.map (fun { Fleche.Doc.Node.Ast.v; _ } -> v) ast in
Lsp.JFleche.RangedSpan.{ range; span }

let to_completed = function
| Fleche.Doc.Completion.Yes range ->
{ Lsp.JFleche.CompletionStatus.status = `Yes; range }
| Stopped range -> { status = `Stopped; range }
| Failed range | FailedPermanent range -> { status = `Failed; range }

let request ~doc =
let { Fleche.Doc.nodes; completed; _ } = doc in
let spans = List.map to_span nodes in
let completed = to_completed completed in
Lsp.JFleche.FlecheDocument.({ spans; completed } |> to_yojson)
8 changes: 8 additions & 0 deletions controller/rq_document.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(************************************************************************)
(* Coq Language Server Protocol -- Document *)
(* Copyright 2019 MINES ParisTech -- Dual License LGPL 2.1 / GPL3+ *)
(* Copyright 2019-2023 Inria -- Dual License LGPL 2.1 / GPL3+ *)
(* Written by: Emilio J. Gallego Arias *)
(************************************************************************)

val request : Requests.document_request
23 changes: 23 additions & 0 deletions editor/code/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,26 @@ export type Pp =
| ["Pp_comment", string[]];

export type PpString = Pp | string;

export interface FlecheDocumentParams {
textDocument: VersionedTextDocumentIdentifier;
}

// Status of the document, Yes if fully checked, range contains the last seen lexical token
interface CompletionStatus {
status: ["Yes" | "Stopped" | "Failed"];
range: Range;
}

// Implementation-specific span information, for now the serialized Ast if present.
type SpanInfo = any;

interface RangedSpan {
range: Range;
span?: SpanInfo;
}

export interface FlecheDocument {
spans: RangedSpan[];
completed: CompletionStatus;
}
4 changes: 4 additions & 0 deletions editor/code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@
{
"command": "coq-lsp.goals",
"title": "Coq LSP: Show Goals at Point"
},
{
"command": "coq-lsp.document",
"title": "Coq LSP: Get document in serialized format, print in console"
}
],
"keybindings": [
Expand Down
21 changes: 20 additions & 1 deletion editor/code/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import {
LanguageClientOptions,
RevealOutputChannelOn,
} from "vscode-languageclient/node";

import {
RequestType,
VersionedTextDocumentIdentifier,
} from "vscode-languageclient";
import { FlecheDocumentParams, FlecheDocument } from "../lib/types";
import { CoqLspClientConfig, CoqLspServerConfig } from "./config";
import { InfoPanel } from "./goals";
import { FileProgressManager } from "./progress";
Expand Down Expand Up @@ -138,8 +142,23 @@ export function activate(context: ExtensionContext): void {

context.subscriptions.push(goalsHook);

const docReq = new RequestType<FlecheDocumentParams, FlecheDocument, void>(
"coq/getDocument"
);

let getDocument = (editor: TextEditor) => {
let uri = editor.document.uri;
let version = editor.document.version;
let textDocument = VersionedTextDocumentIdentifier.create(
uri.toString(),
version
);
let params: FlecheDocumentParams = { textDocument };
client.sendRequest(docReq, params).then((fd) => console.log(fd));
};
coqCommand("restart", restart);
coqEditorCommand("goals", goals);
coqEditorCommand("document", getDocument);

restart();
}
Expand Down
42 changes: 42 additions & 0 deletions etc/doc/PROTOCOL.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,45 @@ interface CoqFileProgressParams {
#### Changelog

- v0.1.1: exact copy from Lean protocol (spec under Apache License)

### Document Ast Request

The `coq/getDocument` request returns a serialized version of Fleche's
document. It is modelled after LSP's standard
`textDocument/documentSymbol`, but returns instead the full document
contents as understood by Flèche.

Caveats: Flèche notion of document is evolving, in particular you
should not assume that the document will remain a list, but it will
become a tree at some point.

```typescript
interface FlecheDocumentParams {
textDocument: VersionedTextDocumentIdentifier;
}
```

```typescript
// Status of the document, Yes if fully checked, range contains the last seen lexical token
interface CompletionStatus {
status : ['Yes' | 'Stopped' | 'Failed']
range : Range
};

// Implementation-specific span information, for now the serialized Ast if present.
type SpanInfo = any;

interface RangedSpan {
range : Range;
span?: SpanInfo
};

interface FlecheDocument {
spans: RangedSpan[];
completed : CompletionStatus
};
```

#### Changelog

- v0.1.6: initial version
7 changes: 7 additions & 0 deletions lsp/jCoq.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,10 @@ module Goals = struct

type 'pp reified_pp = ('pp reified_goal, 'pp) goals [@@deriving to_yojson]
end

module Ast = struct
type t = Coq.Ast.t

let to_yojson x =
Serlib.Ser_vernacexpr.vernac_control_to_yojson (Coq.Ast.to_coq x)
end
25 changes: 25 additions & 0 deletions lsp/jFleche.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
(************************************************************************)

module Pp = JCoq.Pp
module Ast = JCoq.Ast
module Lang = JLang

module Config = struct
Expand Down Expand Up @@ -172,3 +173,27 @@ module CompletionData = struct
}
[@@deriving yojson]
end

module CompletionStatus = struct
type t =
{ status : [ `Yes | `Stopped | `Failed ]
; range : Lang.Range.t
}
[@@deriving yojson]
end

module RangedSpan = struct
type t =
{ range : Lang.Range.t
; span : Ast.t option [@default None]
}
[@@deriving to_yojson]
end

module FlecheDocument = struct
type t =
{ spans : RangedSpan.t list
; completed : CompletionStatus.t
}
[@@deriving to_yojson]
end
25 changes: 25 additions & 0 deletions lsp/jFleche.mli
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,28 @@ module CompletionData : sig
}
[@@deriving yojson]
end

(** Coq-lsp-specific *)
module CompletionStatus : sig
type t =
{ status : [ `Yes | `Stopped | `Failed ]
; range : Lang.Range.t
}
[@@deriving yojson]
end

module RangedSpan : sig
type t =
{ range : Lang.Range.t
; span : Coq.Ast.t option [@default None]
}
[@@deriving to_yojson]
end

module FlecheDocument : sig
type t =
{ spans : RangedSpan.t list
; completed : CompletionStatus.t
}
[@@deriving to_yojson]
end

0 comments on commit 1c8535e

Please sign in to comment.