Skip to content
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

Improving doc comments syntax highlighting in hovering #545

Open
aspeddro opened this issue Aug 3, 2022 · 7 comments
Open

Improving doc comments syntax highlighting in hovering #545

aspeddro opened this issue Aug 3, 2022 · 7 comments

Comments

@aspeddro
Copy link
Contributor

aspeddro commented Aug 3, 2022

After #97 and #525 have been merged we can provider basic syntax highlight when hovering.

Current state

image

Apparently vscode does not detect ```res example as rescript syntax. In markdown highlighting works.

image

Proposal

Convert ```res example to rescript. This can be done on the client side `client/src/extension.ts`, but I'm more +1 to do it in analysis bin as it wouldn't affect other clients.

As the return is markdown we could create sections using the word after ```res?

From:

```res example

To:

## Example\n```rescript

Example:

From:

Constructs a RegExp object (Js.Re.t) from a `string`.
Regex literals `%re("/.../")` should generally be preferred, but `fromString`
is useful when you need to dynamically construct a regex using strings,
exactly like when you do so in JavaScript.

```res example
let firstReScriptFileExtension = (filename, content) => {
  let result = Js.Re.fromString(filename ++ "\.(res|resi)")->Js.Re.exec_(content)
  switch result {
  | Some(r) => Js.Nullable.toOption(Js.Re.captures(r)[1])
  | None => None
  }
}

// outputs "res"
firstReScriptFileExtension("School", "School.res School.resi Main.js School.bs.js")
```

To:

Constructs a RegExp object (Js.Re.t) from a `string`.
Regex literals `%re("/.../")` should generally be preferred, but `fromString`
is useful when you need to dynamically construct a regex using strings,
exactly like when you do so in JavaScript.

## Example

```rescript
let firstReScriptFileExtension = (filename, content) => {
  let result = Js.Re.fromString(filename ++ "\.(res|resi)")->Js.Re.exec_(content)
  switch result {
  | Some(r) => Js.Nullable.toOption(Js.Re.captures(r)[1])
  | None => None
  }
}

// outputs "res"
firstReScriptFileExtension("School", "School.res School.resi Main.js School.bs.js")
```

Below an example after convert res to rescript. Note: indentation at the beginning of the line are preserved, we must remove them.

image

@cristianoc
Copy link
Collaborator

CC @zth @ryyppy

@ryyppy
Copy link
Member

ryyppy commented Aug 4, 2022

It doesn't detect "res" for syntax highlighting? what happens if it's called "rescript example"?

According to the markdown spec, the meta tags should not impact the language detection.

1 similar comment
@ryyppy
Copy link
Member

ryyppy commented Aug 4, 2022

It doesn't detect "res" for syntax highlighting? what happens if it's called "rescript example"?

According to the markdown spec, the meta tags should not impact the language detection.

@aspeddro
Copy link
Contributor Author

aspeddro commented Aug 4, 2022

It doesn't detect "res" for syntax highlighting?

No.

What happens if it's called "rescript example"?

Doesn't work either

image

textDocument/hover response

{
  jsonrpc: '2.0',
  id: 52,
  result: {
    contents: '```rescript\n' +
      'string => Js.Re.t\n' +
      '```\n' +
      '\n' +
      '\n' +
      '  Constructs a RegExp object (Js.Re.t) from a `string`.\n' +
      '  Regex literals `%re("/.../")` should generally be preferred, but `fromString`\n' +
      '  is useful when you need to dynamically construct a regex using strings,\n' +
      '  exactly like when you do so in JavaScript.\n' +
      '\n' +
      '```rescript example\n' +
      '\n' +
      '  let firstReScriptFileExtension = (filename, content) => {\n' +
      '    let result = Js.Re.fromString(filename ++ "\\.(res|resi)")->Js.Re.exec_(content)\n' +
      '    switch result {\n' +
      '    | Some(r) => Js.Nullable.toOption(Js.Re.captures(r)[1])\n' +
      '    | None => None\n' +
      '    }\n' +
      '  }\n' +
      '\n' +
      '  // outputs "res"\n' +
      '  firstReScriptFileExtension("School", "School.res School.resi Main.js School.bs.js")\n' +
      '  ```\n'
  }
}

Rust analyzer and ocaml lsp render with syntax highlights

rust_analyzer use ```rust

[Trace - 5:26:15 PM] Received response 'textDocument/hover - (14)' in 2ms.
Result: {
    "contents": {
        "kind": "markdown",
        "value": "\n```rust\nstd::macros\n```\n\n```rust\nmacro_rules! println\n```\n\n---\n\nPrints to the standard output, with a newline.\n\nOn all platforms, the newline is the LINE FEED character (`\\n`/`U+000A`) alone\n(no additional CARRIAGE RETURN (`\\r`/`U+000D`)).\n\nUse the [`format`](https://doc.rust-lang.org/nightly/alloc/macros/macro.format.html) syntax to write data to the standard output.\nSee [`std::fmt`] for more information.\n\nUse `println!` only for the primary output of your program. Use\n[`eprintln`] instead to print error and progress messages.\n\n# Panics\n\nPanics if writing to [`io::stdout`] fails.\n\n# Examples\n\n```rust\nprintln!(); // prints just a newline\nprintln!(\"hello there!\");\nprintln!(\"format {} arguments\", \"some\");\n```"
    },
    "range": {
        "start": {
            "line": 16,
            "character": 4
        },
        "end": {
            "line": 16,
            "character": 11
        }
    }
}

ocamllsp use ```ocaml

[Trace - 5:32:27 PM] Received response 'textDocument/hover - (7)' in 1ms.
Result: {
    "contents": {
        "kind": "markdown",
        "value": "```ocaml\ntype inlayHint = {\n  position : position;\n  label : string;\n  kind : int;\n  paddingLeft : bool;\n  paddingRight : bool;\n}\n```"
    },
    "range": {
        "start": {
            "line": 10,
            "character": 0
        },
        "end": {
            "line": 16,
            "character": 1
        }
    }
}

@Minnozz
Copy link
Contributor

Minnozz commented Aug 6, 2022

"grammars": [
{
"language": "rescript",
"scopeName": "source.rescript",
"path": "./grammars/rescript.tmLanguage.json"
},
{
"scopeName": "markdown.rescript.codeblock",
"path": "./grammars/rescript.markdown.json",
"injectTo": [
"text.html.markdown"
],
"embeddedLanguages": {
"meta.embedded.block.rescript": "rescript"
}
}
],

I can't find any reference to res here, should that name also be registered in some way?

@Minnozz
Copy link
Contributor

Minnozz commented Aug 6, 2022

Ah, here:

"begin": "(^|\\G)(\\s*)(\\`{3,}|~{3,})\\s*(?i:(res|rescript)(\\s+[^`~]*)?$)",

@aspeddro
Copy link
Contributor Author

aspeddro commented Nov 5, 2022

I've been exploring this issue using omd to format doc comments. Involves adding a dependency to the binary.

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

No branches or pull requests

4 participants