Skip to content

Commit

Permalink
WIP - suuport inlay for Attribute value
Browse files Browse the repository at this point in the history
resolves asciidoctor#731

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier committed Jun 30, 2023
1 parent 6a03eeb commit fbe4e6f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Improvements

- provide folding for list of sibling attributes by @apupier (#719)
- provide inlay hints for attribute values (#729)

### Bug fixes

Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { BuiltinDocumentAttributeProvider } from './features/builtinDocumentAttr
import AsciidocFoldingRangeProvider from './features/foldingProvider'
import { AntoraSupportManager } from './features/antora/antoraSupport'
import { DropImageIntoEditorProvider } from './features/dropIntoEditor'
import { AttributeInlayHintProvider } from './features/attributeInlayHintProvider'

export async function activate (context: vscode.ExtensionContext) {
// Set context as a global as some tests depend on it
Expand Down Expand Up @@ -62,6 +63,7 @@ export async function activate (context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.languages.registerCompletionItemProvider(selector, new BuiltinDocumentAttributeProvider(), ':'))
context.subscriptions.push(vscode.languages.registerFoldingRangeProvider(selector, new AsciidocFoldingRangeProvider()))
context.subscriptions.push(vscode.languages.registerDocumentDropEditProvider(selector, new DropImageIntoEditorProvider()))
context.subscriptions.push(vscode.languages.registerInlayHintsProvider(selector, new AttributeInlayHintProvider()))
const previewSecuritySelector = new PreviewSecuritySelector(cspArbiter, previewManager)
const commandManager = new CommandManager()
context.subscriptions.push(commandManager)
Expand Down
29 changes: 29 additions & 0 deletions src/features/attributeInlayHintProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CancellationToken, Event, InlayHint, InlayHintKind, InlayHintsProvider, Position, ProviderResult, Range, TextDocument } from 'vscode'
import { AsciidocParser } from '../asciidocParser'
//import { AsciidocParser } from '../asciidocParser'

export class AttributeInlayHintProvider implements InlayHintsProvider {
onDidChangeInlayHints?: Event<void>
provideInlayHints (textDocument: TextDocument, range: Range, _token: CancellationToken): ProviderResult<InlayHint[]> {
const inlayHints: InlayHint[] = []
const startLine = range.start.line
const endLine = range.end.line
const document = AsciidocParser.load(textDocument)
for (let index = startLine; index < endLine; index++) {
const lineText = textDocument.lineAt(index).text
console.log(lineText)
const matches = /{[\w-]+}.*/.exec(lineText)
if (matches !== null) {
matches.forEach((value, index, array) => {
console.log('###' + value + index + array)
inlayHints.push(new InlayHint(new Position(index, lineText.indexOf(value) + value.length - 1), document.getAttribute(value)))
})
}
}
return inlayHints
}

resolveInlayHint? (hint: InlayHint, _token: CancellationToken): ProviderResult<InlayHint> {
return hint
}
}
31 changes: 31 additions & 0 deletions src/test/attributeInlayHintProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import 'mocha'
import * as vscode from 'vscode'
import assert from 'assert'
import { Position, Range } from 'vscode'
import { AttributeInlayHintProvider } from '../features/attributeInlayHintProvider'

let root

suite('Attribute Inlay Hint Provider', () => {
let createdFiles: vscode.Uri[] = []
setup(() => {
root = vscode.workspace.workspaceFolders[0].uri.fsPath
})
teardown(async () => {
for (const createdFile of createdFiles) {
await vscode.workspace.fs.delete(createdFile)
}
createdFiles = []
})
test('Should return attribute value as inlay', async () => {
const fileWithInlayHint = vscode.Uri.file(`${root}/fileWithAttributeValueToInlay.adoc`)
await vscode.workspace.fs.writeFile(fileWithInlayHint, Buffer.from(`:my-attribute-to-inlay: value to inlay
{my-attribute-to-inlay}
`))
createdFiles.push(fileWithInlayHint)

const file = await vscode.workspace.openTextDocument(fileWithInlayHint)
new AttributeInlayHintProvider().provideInlayHints(file, new Range(new Position(2, 0), new Position(2, 24)), undefined)
})
})

0 comments on commit fbe4e6f

Please sign in to comment.