Skip to content

Commit

Permalink
fix: max length on hover (#24)
Browse files Browse the repository at this point in the history
* fix hover

* emergency patch

---------

Co-authored-by: Myles Murphy <[email protected]>
  • Loading branch information
mylesmmurphy and Myles Murphy authored May 6, 2024
1 parent c94b481 commit 28a9252
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "View \"prettified\" and nested types in hover tooltips and sidebar.",
"publisher": "MylesMurphy",
"license": "MIT",
"version": "0.0.18",
"version": "0.0.19",
"icon": "assets/logo.png",
"engines": {
"vscode": "^1.44.0"
Expand Down
8 changes: 6 additions & 2 deletions src/hover-provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode'
import * as ts from 'typescript'
import { prettifyType } from './prettify/prettify-type'
import { EXTENSION_ID } from './consts'
import { EXTENSION_ID, MARKDOWN_MAX_LENGTH } from './consts'
import { getProject } from './project-cache'
import { washString } from './prettify/prettify-functions'

Expand All @@ -19,7 +19,7 @@ export function registerHoverProvider (context: vscode.ExtensionContext): void {
const offset = document.offsetAt(position)
const fileName = document.fileName

const typeString = prettifyType(fileName, content, offset)
let typeString = prettifyType(fileName, content, offset)

if (typeString === undefined) return

Expand All @@ -33,6 +33,10 @@ export function registerHoverProvider (context: vscode.ExtensionContext): void {
if (washString(quickInfoText).includes(washString(typeString))) return
}

if (typeString.length > MARKDOWN_MAX_LENGTH) {
typeString = typeString.substring(0, MARKDOWN_MAX_LENGTH) + '...'
}

const hoverText = new vscode.MarkdownString()
hoverText.appendCodeblock(typeString, document.languageId)
return new vscode.Hover(hoverText)
Expand Down
6 changes: 1 addition & 5 deletions src/prettify/prettify-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as vscode from 'vscode'
import { SyntaxKind } from 'ts-morph'
import { ulid } from 'ulid'

import { EXTENSION_ID, MARKDOWN_MAX_LENGTH } from '../consts'
import { EXTENSION_ID } from '../consts'
import { buildDeclarationString, getPrettifyType, formatDeclarationString } from './prettify-functions'
import { getProject } from '../project-cache'
import { ScriptElementKind } from 'typescript'
Expand Down Expand Up @@ -86,10 +86,6 @@ export function prettifyType (fileName: string, content: string, offset: number)
// Issue: Prettify doesn't always work for functions or complex types
if (prettifiedTypeString === 'any') return

if (prettifiedTypeString.length > MARKDOWN_MAX_LENGTH) {
prettifiedTypeString = prettifiedTypeString.substring(0, MARKDOWN_MAX_LENGTH) + '...'
}

const declarationString = buildDeclarationString(parentNodeKind, nodeText, prettifiedTypeString)
const typeString = formatDeclarationString(declarationString, typeIndentation)

Expand Down
8 changes: 6 additions & 2 deletions src/webviews/type-provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode'

import { prettifyType } from '../prettify/prettify-type'
import { EXTENSION_ID, IS_DEV } from '../consts'
import { EXTENSION_ID, IS_DEV, MARKDOWN_MAX_LENGTH } from '../consts'

export class TypeProvider implements vscode.WebviewViewProvider {
private readonly extensionContext: vscode.ExtensionContext
Expand Down Expand Up @@ -74,7 +74,11 @@ export class TypeProvider implements vscode.WebviewViewProvider {
const offset = document.offsetAt(position)

updateWebview('', true)
const formattedTypeString = prettifyType(fileName, content, offset) ?? ''
let formattedTypeString = prettifyType(fileName, content, offset) ?? ''

if (formattedTypeString.length > MARKDOWN_MAX_LENGTH) {
formattedTypeString = formattedTypeString.substring(0, MARKDOWN_MAX_LENGTH) + '...'
}
updateWebview(formattedTypeString)
}

Expand Down

0 comments on commit 28a9252

Please sign in to comment.