logWorker.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import type * as Monaco from 'monaco-editor'
  2. // 同Monaco.MarkerSeverity
  3. enum MarkerSeverity {
  4. Hint = 1,
  5. Info = 2,
  6. Warning = 4,
  7. Error = 8,
  8. }
  9. export class LogWorker {
  10. constructor(
  11. private readonly _ctx: Monaco.worker.IWorkerContext,
  12. private readonly _createData: ICreateData,
  13. ) {
  14. console.log({ ctx: _ctx, createData: _createData })
  15. }
  16. private _getText(uri: string): string {
  17. const model = this._ctx.getMirrorModels().find((m) => m.uri.toString() === uri)
  18. return model?.getValue() ?? ''
  19. }
  20. // TODO 待实现,可实现告警码提示
  21. async provideInlayHints(
  22. uri: string,
  23. start: number,
  24. end: number,
  25. ): Promise<readonly Monaco.languages.InlayHint[]> {
  26. return []
  27. }
  28. async doValidation(uri: string): Promise<Monaco.editor.IMarkerData[]> {
  29. const text = this._getText(uri)
  30. const markers: Monaco.editor.IMarkerData[] = []
  31. const lines = text.split('\n')
  32. lines.forEach((line, index) => {
  33. if (line.includes('[E]') || line.includes('[Error]') || line.includes('[ERROR]')) {
  34. markers.push({
  35. severity: MarkerSeverity.Error,
  36. message: 'Error log detected',
  37. startLineNumber: index + 1,
  38. endLineNumber: index + 1,
  39. startColumn: 1,
  40. endColumn: line.length + 1,
  41. })
  42. }
  43. })
  44. return markers
  45. }
  46. }
  47. export interface ICreateData {
  48. languageId: string
  49. }
  50. export function create(ctx: Monaco.worker.IWorkerContext, createData: ICreateData): LogWorker {
  51. return new LogWorker(ctx, createData)
  52. }