|
| 1 | +import { Hover, TextDocument, HoverParams } from 'vscode-languageserver'; |
| 2 | +import { BaseHoverProvider } from '../BaseHoverProvider'; |
| 3 | +import { getCssLanguageService } from '../../plugins/css'; |
| 4 | +import { Position } from 'vscode-json-languageservice'; |
| 5 | +import { LiquidHtmlNode } from '@shopify/liquid-html-parser'; |
| 6 | +import { DocumentManager } from '../../documents'; |
| 7 | + |
| 8 | +export class CssHoverProvider implements BaseHoverProvider { |
| 9 | + constructor(private documentManager: DocumentManager) {} |
| 10 | + |
| 11 | + async hover(currentNode: LiquidHtmlNode, ancestors: LiquidHtmlNode[], params: HoverParams): Promise<Hover | null> { |
| 12 | + const node = currentNode; |
| 13 | + const document = this.documentManager.get(params.textDocument.uri); |
| 14 | + |
| 15 | + if (document && node && 'stylesheet' in document && document.stylesheet && document.stylesheet.cssStart >= node.position.start && document.stylesheet.cssEnd <= node.position.end) { |
| 16 | + const cssLanguageService = getCssLanguageService('css'); |
| 17 | + const offset = document.stylesheet.cssStart; |
| 18 | + const position = offsetToPosition(document.stylesheet.source, offset); |
| 19 | + const stylesheet = cssLanguageService.parseStylesheet(document.stylesheet.source); |
| 20 | + const information = cssLanguageService.doHover(document.stylesheet.source, position, stylesheet); |
| 21 | + return information ? { |
| 22 | + contents: information.contents, |
| 23 | + range: information.range ? { |
| 24 | + start: offsetToPosition(document.stylesheet.source, positionToOffset(document.stylesheet.source, information.range.start) + offset), |
| 25 | + end: offsetToPosition(document.stylesheet.source, positionToOffset(document.stylesheet.source, information.range.end) + offset), |
| 26 | + } : undefined, |
| 27 | + } : null; |
| 28 | + } else { |
| 29 | + return null; |
| 30 | + } |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +function offsetToPosition(source: TextDocument, offset: number): Position { |
| 35 | + const line = source.getText().slice(0, offset).split('\n').length - 1; |
| 36 | + const character = offset - source.getText().slice(0, offset).lastIndexOf('\n'); |
| 37 | + return { line, character }; |
| 38 | +} |
| 39 | + |
| 40 | +function positionToOffset(source: TextDocument, position: Position): number { |
| 41 | + return source.getText().slice(0, position.line).lastIndexOf('\n') + position.character; |
| 42 | +} |
0 commit comments