Skip to content
This repository was archived by the owner on Mar 19, 2024. It is now read-only.

Commit e46be55

Browse files
authored
Workaround missing markdown syntax highlighting (#171)
Convert datatips/hover "```rust\ncode\n```" into text & rust-snippets of 'code' to allow syntax highlighting of these snippets.
1 parent 4bb9824 commit e46be55

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

lib/index.js

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -572,21 +572,58 @@ class RustLanguageClient extends AutoLanguageClient {
572572
return provide
573573
}
574574

575-
// Workaround #133 that affects stable rust 1.35.0
576-
async getCodeFormat(...args) {
577-
const edits = await super.getCodeFormat(...args)
578-
for (const edit of edits) {
579-
const end = edit && edit.oldRange && edit.oldRange.end
580-
if (end && end.column > 18e18) {
581-
end.row += 1
582-
end.column = 0
583-
edit.newText += (process.platform === "win32" ? "\r\n" : "\n")
575+
/**
576+
* Extend base-class to workaround the limited markdown support.
577+
* @param {TextEditor} editor
578+
* @param {Point} point
579+
* @returns {Promise<atomIde.Datatip | null>}
580+
*/
581+
async getDatatip(editor, ...args) {
582+
let datatip = await super.getDatatip(editor, ...args)
583+
try {
584+
if (datatip) {
585+
datatip.markedStrings = datatip.markedStrings
586+
.flatMap(m => {
587+
if (!m.grammar && m.type === "markdown" && m.value) {
588+
return convertMarkdownToSnippets(m.value)
589+
} else {
590+
return m
591+
}
592+
})
593+
.filter(m => m.value)
584594
}
595+
} catch (e) {
596+
console.error("Error processing datatip", e)
585597
}
586-
return edits
598+
return datatip
587599
}
588600
}
589601

602+
/**
603+
* Convert "foo\n```rust\nHashSet<u32, String>\n```\nbar"
604+
* to [{type: "markdown", value: "foo"}, {type:"snippet", value: "HashSet<u32, String>", ...}, ...]
605+
* @param {string} value
606+
* @returns {object[]}
607+
*/
608+
function convertMarkdownToSnippets(value) {
609+
// even indices are text, odds are rust snippets
610+
return value.split(/\s*```rust\s*((?:.|\s)+?)\s*```/)
611+
.map((bit, index) => {
612+
if (index % 2 == 0) {
613+
return {
614+
type: "markdown",
615+
value: bit,
616+
}
617+
} else {
618+
return {
619+
type: "snippet",
620+
grammar: atom.grammars.grammarForScopeName("source.rust"),
621+
value: bit,
622+
}
623+
}
624+
})
625+
}
626+
590627
// override windows specific implementations
591628
if (process.platform === "win32") {
592629
// handle different slashes

0 commit comments

Comments
 (0)