decorationProvider.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. import * as vscode from 'vscode';
  6. import { readFromFile } from './utilities/directoryUtilities';
  7. import * as path from 'path';
  8. import * as os from 'os';
  9. const DECORATION_INCLUDE_PATHS: vscode.FileDecoration =
  10. new vscode.FileDecoration(
  11. '✔',
  12. 'Included',
  13. new vscode.ThemeColor('list.highlightForeground')
  14. );
  15. const DECORATION_EXCLUDE_FILES: vscode.FileDecoration =
  16. new vscode.FileDecoration(
  17. '✗',
  18. 'Excluded',
  19. new vscode.ThemeColor('list.errorForeground')
  20. );
  21. export class DecorationProvider implements vscode.FileDecorationProvider {
  22. private disposables: vscode.Disposable[] = [];
  23. public onDidChangeFileDecorations: vscode.Event<
  24. vscode.Uri | vscode.Uri[] | undefined
  25. >;
  26. private eventEmitter: vscode.EventEmitter<vscode.Uri | vscode.Uri[]>;
  27. constructor() {
  28. this.eventEmitter = new vscode.EventEmitter();
  29. this.onDidChangeFileDecorations = this.eventEmitter.event;
  30. this.disposables.push(
  31. vscode.window.registerFileDecorationProvider(this)
  32. );
  33. }
  34. public provideFileDecoration(
  35. uri: vscode.Uri
  36. ): vscode.ProviderResult<vscode.FileDecoration> {
  37. const currentPrjDir =
  38. os.platform() === 'win32'
  39. ? (vscode.workspace.workspaceFolders?.[0].uri.fsPath as string)
  40. : os.platform() === 'linux' || os.platform() === 'darwin'
  41. ? (vscode.workspace.workspaceFolders?.[0].uri.path as string)
  42. : '';
  43. const pathRelative = (uri.fsPath ? uri.fsPath : uri.toString()).replace(
  44. currentPrjDir,
  45. '..'
  46. );
  47. const prjConfigDir = path.join(currentPrjDir, '.wamr');
  48. const configFilePath = path.join(
  49. prjConfigDir,
  50. 'compilation_config.json'
  51. );
  52. if (readFromFile(configFilePath) !== '') {
  53. const configData = JSON.parse(readFromFile(configFilePath));
  54. const includePathArr = configData['includePaths'];
  55. const excludeFileArr = configData['excludeFiles'];
  56. if (includePathArr.indexOf(pathRelative) > -1) {
  57. return DECORATION_INCLUDE_PATHS;
  58. } else if (excludeFileArr.indexOf(pathRelative) > -1) {
  59. return DECORATION_EXCLUDE_FILES;
  60. }
  61. }
  62. }
  63. public dispose(): void {
  64. this.disposables.forEach(d => d.dispose());
  65. }
  66. public updateDecorationsForSource(uri: vscode.Uri): void {
  67. this.eventEmitter.fire(uri);
  68. }
  69. }
  70. export const decorationProvider: DecorationProvider = new DecorationProvider();