Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"scopeName": "source.type",
"path": "./syntaxes/type.tmGrammar.json"
}
],
"markdown.previewStyles": [
"./dist/extension.css"
]
},
"scripts": {
Expand Down
21 changes: 18 additions & 3 deletions src/components/title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import { Diagnostic } from "vscode-languageserver-types";
import { compressToEncodedURIComponent, d } from "../utils";
import { KNOWN_ERROR_NUMBERS } from "./consts/knownErrorNumbers";
import { miniLine } from "./miniLine";
import { Uri } from 'vscode';
import { PRETTY_TS_ERRORS_SCHEME } from '../provider/textDocumentProvider';

export const title = (diagnostic: Diagnostic) => d/*html*/ `
export const title = (diagnostic: Diagnostic, uri: Uri) => d/*html*/ `
<span style="color:#f96363;">⚠ Error </span>${
typeof diagnostic.code === "number"
? d/*html*/ `
<span style="color:#5f5f5f;">
(TS${diagnostic.code})
${errorCodeExplanationLink(diagnostic.code)} |
(TS${diagnostic.code})
${errorCodeExplanationLink(diagnostic.code)} |
${errorMessageTranslationLink(diagnostic.message)}
Copy link
Owner

Choose a reason for hiding this comment

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

Can we add | before the new icon?

Comment on lines +14 to 15
Copy link
Owner

Choose a reason for hiding this comment

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

The links are not working in the preview panel, do you know why?

Copy link
Author

Choose a reason for hiding this comment

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

good call out, will look into it and report back

${errorMessageInANewFile(diagnostic, uri)}
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure why, but if a panel is opened and I click on another one, it stays on the previous one.
Is there something to do about it?

</span>
`
: ""
Expand All @@ -37,3 +40,15 @@ export const errorMessageTranslationLink = (message: Diagnostic["message"]) => {
</span>
</a>`;
};

export const errorMessageInANewFile = (diagnostic: Diagnostic, uri: Uri) => {
const range = `${diagnostic.range.start.line}:${diagnostic.range.start.character}-${diagnostic.range.end.line}:${diagnostic.range.end.character}`;
const virtualFileUri = Uri.parse(`${PRETTY_TS_ERRORS_SCHEME}:${encodeURIComponent(uri.fsPath + '.md')}?range=${encodeURIComponent(range)}`);
const args = [virtualFileUri];
const href = Uri.parse(`command:markdown.showPreview?${encodeURIComponent(JSON.stringify(args))}`);
return d/*html*/ `
<a title="Open in new tab" href="${href}">
<span class="codicon codicon-new-file">
</span>
</a>`;
};
3 changes: 3 additions & 0 deletions src/extension.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
span.hljs-title.class_ {
color: #4ec9b0
}
Comment on lines +1 to +3
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure it'll look good for every theme.
Is it crucial?

9 changes: 8 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
MarkdownString,
Range,
window,
workspace,
} from "vscode";
import { createConverter } from "vscode-languageclient/lib/common/codeConverter";
import { formatDiagnostic } from "./format/formatDiagnostic";
Expand All @@ -12,6 +13,8 @@ import { hoverProvider } from "./provider/hoverProvider";
import { registerSelectedTextHoverProvider } from "./provider/selectedTextHoverProvider";
import { uriStore } from "./provider/uriStore";
import { has } from "./utils";
import { PRETTY_TS_ERRORS_SCHEME, textDocumentContentProvider } from './provider/textDocumentProvider';
import './extension.css'

const cache = new Map();

Expand All @@ -21,6 +24,10 @@ export function activate(context: ExtensionContext) {

registerSelectedTextHoverProvider(context);

context.subscriptions.push(
workspace.registerTextDocumentContentProvider(PRETTY_TS_ERRORS_SCHEME, textDocumentContentProvider),
);

context.subscriptions.push(
languages.onDidChangeDiagnostics(async (e) => {
e.uris.forEach((uri) => {
Expand Down Expand Up @@ -49,7 +56,7 @@ export function activate(context: ExtensionContext) {

if (!formattedMessage) {
const markdownString = new MarkdownString(
formatDiagnostic(converter.asDiagnostic(diagnostic), prettify)
formatDiagnostic(converter.asDiagnostic(diagnostic), uri, prettify)
);

markdownString.isTrusted = true;
Expand Down
1 change: 1 addition & 0 deletions src/format/embedSymbolLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function embedSymbolLinks(diagnostic: Diagnostic): Diagnostic {
if (!symbol) {
return diagnostic;
}

return {
...diagnostic,
message: diagnostic.message.replaceAll(
Expand Down
4 changes: 3 additions & 1 deletion src/format/formatDiagnostic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { d } from "../utils";
import { embedSymbolLinks } from "./embedSymbolLinks";
import { formatDiagnosticMessage } from "./formatDiagnosticMessage";
import { identSentences } from "./identSentences";
import { Uri } from 'vscode';

export function formatDiagnostic(
diagnostic: Diagnostic,
uri: Uri,
format: (type: string) => string
) {
const newDiagnostic = embedSymbolLinks(diagnostic);

return d/*html*/ `
${title(diagnostic)}
${title(diagnostic, uri)}
<span>
${formatDiagnosticMessage(identSentences(newDiagnostic.message), format)}
</span>
Expand Down
14 changes: 11 additions & 3 deletions src/provider/selectedTextHoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { miniLine } from "../components";
import { formatDiagnostic } from "../format/formatDiagnostic";
import { prettify } from "../format/prettify";
import { d } from "../utils";
import { uriStore } from './uriStore';

/**
* Register an hover provider in debug only.
Expand Down Expand Up @@ -47,14 +48,21 @@ export function registerSelectedTextHoverProvider(context: ExtensionContext) {
source: "ts",
code: 1337,
}),
document.uri,
prettify
)
),
]
: [];

contents[0].isTrusted = true;
contents[0].supportHtml = true;
if (contents.length) {
contents[0].isTrusted = true;
contents[0].supportHtml = true;
}

if (range) {
uriStore[document.uri.fsPath] = [{ range, contents }];
}

return {
contents,
Expand All @@ -65,7 +73,7 @@ export function registerSelectedTextHoverProvider(context: ExtensionContext) {
);
}

const debugHoverHeader = d/*html*/ `
const debugHoverHeader = d/*html*/ `
<span style="color:#f96363;">
<span class="codicon codicon-debug"></span>
Formatted selected text (debug only)
Expand Down
43 changes: 43 additions & 0 deletions src/provider/textDocumentProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Range, TextDocumentContentProvider } from 'vscode';
import { uriStore } from './uriStore';

export const PRETTY_TS_ERRORS_SCHEME = 'pretty-ts-errors';

export const textDocumentContentProvider: TextDocumentContentProvider = {
provideTextDocumentContent(uri) {
const searchParams = new URLSearchParams(uri.query);
const fsPath = uri.fsPath.replace(/\.md$/, '');
if (!searchParams.has('range')) {
return `range query parameter is missing for uri: ${uri}`;
}
const items = uriStore[fsPath];
if (!items) {
return `no diagnostics found for ${fsPath}`;
}
const range = createRangeFromString(searchParams.get('range')!);
const item = items.find((item) => {
return item.range.isEqual(range);
});
if (!item) {
return `no diagnostic found for ${fsPath} with range ${JSON.stringify(range)}`;
}
const content = item.contents.map(
(content) => content.value
.replaceAll("```type\n", '```typescript\n'))
Copy link
Owner

Choose a reason for hiding this comment

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

I tried to inject the tmGrammer to the markdown rendered but failed to make it work 😣
I guess there is no other option than this hack, which is cool it's working

.join('\n');
/*
Explanation
At this point the user has seen the tooltip and needs to see the markdown preview
The preview needed ```type to be formatted correctly to the user but this is no longer needed.
The markdown preview needs ```typescript to be formatted correctly.
*/
return content;
},
};

function createRangeFromString(range: string) {
const [start, end] = range.split('-');
const [startLine, startCharacter] = start.split(':').map(Number);
const [endLine, endCharacter] = end.split(':').map(Number);
return new Range(startLine, startCharacter, endLine, endCharacter);
}