Skip to content

Commit

Permalink
Show hint for UNC paths and images on hover
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Jul 4, 2020
1 parent b870dcf commit eb3f636
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/extensions/ReferenceHoverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isLongRef,
getConfigProperty,
getReferenceAtPosition,
isUncPath,
} from '../utils';

export default class ReferenceHoverProvider implements vscode.HoverProvider {
Expand Down Expand Up @@ -39,12 +40,20 @@ export default class ReferenceHoverProvider implements vscode.HoverProvider {
});

if (foundUri && fs.existsSync(foundUri.fsPath)) {
const fileContent = containsImageExt(foundUri.fsPath)
? `![](${vscode.Uri.file(foundUri.fsPath).toString()}|height=${imagePreviewMaxHeight})`
: fs.readFileSync(foundUri.fsPath).toString();
const getContent = () => {
if (containsImageExt(foundUri.fsPath)) {
return `![${
isUncPath(foundUri.fsPath)
? 'UNC paths are not supported for images preview due to VSCode Content Security Policy. Use markdown preview instead or open image via cmd (ctrl) + click.'
: ''
}](${vscode.Uri.file(foundUri.fsPath).toString()}|height=${imagePreviewMaxHeight})`;
}

return fs.readFileSync(foundUri.fsPath).toString();
};

return new vscode.Hover(
fileContent,
getContent(),
new vscode.Range(
new vscode.Position(range.start.line, range.start.character + 2),
new vscode.Position(range.end.line, range.end.character - 2),
Expand Down
4 changes: 4 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,7 @@ export const getFileUrlForMarkdownPreview = (filePath: string): string =>

export const getImgUrlForMarkdownPreview = (imagePath: string): string =>
`vscode-resource://${vscode.Uri.file(imagePath).toString().replace('file:', 'file')}`;

const uncPathRegex = /^[\\\/]{2,}[^\\\/]+[\\\/]+[^\\\/]+/;

export const isUncPath = (path: string): boolean => uncPathRegex.test(path);

0 comments on commit eb3f636

Please sign in to comment.