Skip to content

Commit

Permalink
Fix: Bash IDE relative paths
Browse files Browse the repository at this point in the history
  • Loading branch information
idillon-sfl committed Feb 6, 2024
1 parent 1c67851 commit f866f9f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
35 changes: 31 additions & 4 deletions client/src/language/middlewareHover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
* ------------------------------------------------------------------------------------------ */

import { type HoverMiddleware } from 'vscode-languageclient'
import { type Hover, commands, workspace, MarkdownString } from 'vscode'
import { type Hover, commands, workspace, MarkdownString, type TextDocument } from 'vscode'

import { getEmbeddedLanguageDocPosition } from './utils'
import { embeddedLanguageDocsManager } from './EmbeddedLanguageDocsManager'
import { type EmbeddedLanguageDocInfos, embeddedLanguageDocsManager } from './EmbeddedLanguageDocsManager'
import { requestsManager } from './RequestManager'
import path from 'path'

export const middlewareProvideHover: HoverMiddleware['provideHover'] = async (document, position, token, next) => {
const nextResult = await next(document, position, token)
Expand All @@ -30,12 +31,12 @@ export const middlewareProvideHover: HoverMiddleware['provideHover'] = async (do
embeddedLanguageDocInfos.characterIndexes,
position
)
const result = await commands.executeCommand<Hover[]>(
const hovers = await commands.executeCommand<Hover[]>(
'vscode.executeHoverProvider',
embeddedLanguageDocInfos.uri,
adjustedPosition
)
return result.find((hover) => {
const selectedHover = hovers.find((hover) => {
const contents = hover.contents
return contents.find((content) => {
if (content instanceof MarkdownString) {
Expand All @@ -44,4 +45,30 @@ export const middlewareProvideHover: HoverMiddleware['provideHover'] = async (do
return content !== ''
})
})
if (selectedHover !== undefined) {
fixBashIdeRelativePath(selectedHover, embeddedLanguageDocInfos, document)
}
return selectedHover
}

// Bash IDE gives relative paths relative to the embedded language document. This path does not make any sense to the user.
// This makes the path relative to the document the user is looking at instead.
const fixBashIdeRelativePath = (hover: Hover, embeddedLanguageDocInfos: EmbeddedLanguageDocInfos, document: TextDocument): void => {
if (embeddedLanguageDocInfos.language !== 'bash') {
return
}

hover.contents.forEach((content) => {
if (content instanceof MarkdownString) {
// ex: Function: **bbwarn** - *defined in ../../../../../../../../../poky/meta/classes-global/logging.bbclass*
const match = content.value.match(/^Function: \*\*\b\w+\b\*\* - \*defined in (?<path>.*\.bbclass)\*/)
const wrongRelativePath = match?.groups?.path
if (wrongRelativePath === undefined) {
return
}
const absolutePath = path.resolve(path.dirname(embeddedLanguageDocInfos.uri.fsPath), wrongRelativePath)
const fixedRelativePath = path.relative(path.dirname(document.uri.fsPath), absolutePath)
content.value = content.value.replace(wrongRelativePath, fixedRelativePath)
}
})
}
8 changes: 4 additions & 4 deletions integration-tests/src/tests/hover.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ suite('Bitbake Hover Test Suite', () => {
await testHover(position, expected)
}).timeout(BITBAKE_TIMEOUT)

test('Hover shows description for function defined into meta/classes-global/logging.bbclass', async () => {
test('Hover shows description for function defined into poky/meta/classes-global/logging.bbclass', async () => {
const position = new vscode.Position(14, 6)
const expected = 'Function: **bbwarn** - *defined in'
const expected = 'Function: **bbwarn** - *defined in ../poky/meta/classes-global/logging.bbclass*'
await testHover(position, expected)
}).timeout(BITBAKE_TIMEOUT)

test('Hover shows description for function defined into meta/classes-global/base.bbclass', async () => {
test('Hover shows description for function defined into poky/meta/classes-global/base.bbclass', async () => {
const position = new vscode.Position(15, 6)
const expected = 'Function: **oe_runmake** - *defined in'
const expected = 'Function: **oe_runmake** - *defined in ../poky/meta/classes-global/base.bbclass*'
await testHover(position, expected)
}).timeout(BITBAKE_TIMEOUT)
})

0 comments on commit f866f9f

Please sign in to comment.