|
| 1 | +import { DecorationOptions, TextEditorDecorationType, Uri, window } from 'vscode'; |
| 2 | +import path from 'path'; |
| 3 | +import TextDocumentDecorationProvider from './TextDocumentDecorationProvider'; |
| 4 | +import { PhpClass } from 'parser/php/PhpClass'; |
| 5 | +import { PhpInterface } from 'parser/php/PhpInterface'; |
| 6 | +import PhpDocumentParser from 'common/php/PhpDocumentParser'; |
| 7 | +import { ClasslikeInfo } from 'common/php/ClasslikeInfo'; |
| 8 | +import MarkdownMessageBuilder from 'common/MarkdownMessageBuilder'; |
| 9 | +import IndexManager from 'indexer/IndexManager'; |
| 10 | +import CronIndexer from 'indexer/cron/CronIndexer'; |
| 11 | +import { Job } from 'indexer/cron/types'; |
| 12 | +import cronstrue from 'cronstrue'; |
| 13 | + |
| 14 | +export default class CronClassDecorationProvider extends TextDocumentDecorationProvider { |
| 15 | + public getType(): TextEditorDecorationType { |
| 16 | + return window.createTextEditorDecorationType({ |
| 17 | + gutterIconPath: path.join(__dirname, 'resources', 'icons', 'cron.svg'), |
| 18 | + gutterIconSize: '80%', |
| 19 | + borderColor: 'rgba(0, 188, 202, 0.5)', |
| 20 | + borderStyle: 'dotted', |
| 21 | + borderWidth: '0 0 1px 0', |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + public async getDecorations(): Promise<DecorationOptions[]> { |
| 26 | + const decorations: DecorationOptions[] = []; |
| 27 | + const phpFile = await PhpDocumentParser.parse(this.document); |
| 28 | + |
| 29 | + const classLikeNode: PhpClass | PhpInterface | undefined = |
| 30 | + phpFile.classes[0] || phpFile.interfaces[0]; |
| 31 | + |
| 32 | + if (!classLikeNode) { |
| 33 | + return decorations; |
| 34 | + } |
| 35 | + |
| 36 | + const classlikeInfo = new ClasslikeInfo(phpFile); |
| 37 | + |
| 38 | + const cronIndexData = IndexManager.getIndexData(CronIndexer.KEY); |
| 39 | + |
| 40 | + if (!cronIndexData) { |
| 41 | + return decorations; |
| 42 | + } |
| 43 | + |
| 44 | + const jobs = cronIndexData.findJobsByInstance(classlikeInfo.getNamespace()); |
| 45 | + |
| 46 | + if (jobs.length === 0) { |
| 47 | + return decorations; |
| 48 | + } |
| 49 | + |
| 50 | + decorations.push(...this.getCronInstanceDecorations(jobs, classlikeInfo)); |
| 51 | + |
| 52 | + return decorations; |
| 53 | + } |
| 54 | + |
| 55 | + private getCronInstanceDecorations( |
| 56 | + jobs: Job[], |
| 57 | + classlikeInfo: ClasslikeInfo |
| 58 | + ): DecorationOptions[] { |
| 59 | + const decorations: DecorationOptions[] = []; |
| 60 | + |
| 61 | + const nameRange = classlikeInfo.getNameRange(); |
| 62 | + |
| 63 | + if (!nameRange) { |
| 64 | + return decorations; |
| 65 | + } |
| 66 | + |
| 67 | + const hoverMessage = MarkdownMessageBuilder.create('Cron Jobs'); |
| 68 | + |
| 69 | + for (const job of jobs) { |
| 70 | + hoverMessage.appendMarkdown(`- [${job.name}](${Uri.file(job.path)}) (${job.method})\n`); |
| 71 | + |
| 72 | + if (job.schedule) { |
| 73 | + hoverMessage.appendMarkdown( |
| 74 | + ` - \`${job.schedule}\` (${cronstrue.toString(job.schedule)})\n` |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + if (job.config_path) { |
| 79 | + hoverMessage.appendMarkdown(` - Config: \`${job.config_path}\`\n`); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + decorations.push({ |
| 84 | + range: nameRange, |
| 85 | + hoverMessage: hoverMessage.build(), |
| 86 | + }); |
| 87 | + |
| 88 | + return decorations; |
| 89 | + } |
| 90 | +} |
0 commit comments