/open_env.py

https://github.com/titoBouzout/Open-Include · Python · 156 lines · 105 code · 31 blank · 20 comment · 33 complexity · 257d4408a5c958d4d0b9bf4714fe3d14 MD5 · raw file

  1. import sublime, sublime_plugin
  2. import os
  3. # Note: This plugin uses 'Verbose' plugin available in 'Package Control' to log some messages for debug purpose but it works fine without.
  4. PluginName = 'Open-Include'
  5. def verbose(**kwargs):
  6. kwargs.update({'plugin_name': PluginName})
  7. sublime.run_command("verbose", kwargs)
  8. class Prefs:
  9. @staticmethod
  10. def load():
  11. print('Load settings: ' + PluginName)
  12. settings = sublime.load_settings(PluginName + '.sublime-settings')
  13. os_specific_settings = {}
  14. if sublime.platform() == 'windows':
  15. os_specific_settings = sublime.load_settings(PluginName + ' (Windows).sublime-settings')
  16. elif sublime.platform() == 'osx':
  17. os_specific_settings = sublime.load_settings(PluginName + ' (OSX).sublime-settings')
  18. else:
  19. os_specific_settings = sublime.load_settings(PluginName + ' (Linux).sublime-settings')
  20. Prefs.environment = os_specific_settings.get('environment', settings.get('environment', []))
  21. Prefs.expand_alias = os_specific_settings.get('expand_alias', settings.get('expand_alias', True))
  22. @staticmethod
  23. def show():
  24. verbose(log="############################################################")
  25. for env in Prefs.environment:
  26. for key, values in env.items():
  27. verbose(log=key + ": " + ';'.join(values))
  28. verbose(log="############################################################")
  29. def plugin_loaded():
  30. Prefs.load()
  31. ### OpenFileFromEnv ###
  32. # Find root directory
  33. # Get base directory with name
  34. # foreach env test if file exit
  35. class OpenFileFromEnvCommand(sublime_plugin.TextCommand):
  36. # Set by is_enabled()
  37. initial_env_name = ''
  38. base_name = ''
  39. # List of existing files in other environments
  40. env_files = []
  41. def run(self, edit):
  42. verbose(log="run()")
  43. verbose(log="initial_env_name: " + self.initial_env_name)
  44. verbose(log="base_name: " + self.base_name)
  45. if len(self.base_name) > 0:
  46. # Create a list of files which exist in other environment
  47. self.env_files = []
  48. for env in Prefs.environment:
  49. for env_name, root_alias in env.items():
  50. # Bypass initial environment
  51. if env_name == self.initial_env_name:
  52. continue
  53. # Loop in path alias of the current environment
  54. available_file_names = []
  55. for root in root_alias:
  56. env_file_name = os.path.join(os.path.expandvars(root), self.base_name)
  57. state = ' '
  58. if os.path.exists(env_file_name):
  59. state = 'X'
  60. if Prefs.expand_alias:
  61. self.env_files.append([env_name, env_file_name])
  62. else:
  63. available_file_names.append(env_file_name)
  64. verbose(log='[%s] %15s %s' % (state, env_name, env_file_name))
  65. if len(available_file_names) > 0:
  66. # available_file_names used only with expand_alias = False
  67. current_id = self.view.id()
  68. is_file_opened = False
  69. # Find the first file of the environment which is already opened in st
  70. for v in self.view.window().views():
  71. if v.id() == current_id or v.file_name() is None:
  72. continue
  73. for file_name in available_file_names:
  74. if file_name.lower() == v.file_name().lower():
  75. self.env_files.append([env_name, file_name])
  76. is_file_opened = True
  77. break
  78. if is_file_opened:
  79. break
  80. # Or choose the file of the environment of the main path
  81. if not is_file_opened:
  82. self.env_files.append([env_name, available_file_names[0]])
  83. if len(self.env_files) > 0:
  84. self.view.window().show_quick_panel(self.env_files, self.quick_panel_done)
  85. else:
  86. sublime.status_message("No file found in other environments")
  87. def quick_panel_done(self, index):
  88. if index > -1:
  89. # Open selected file in an another environment
  90. self.view.window().open_file(self.env_files[index][1])
  91. def is_filename_part_of_env(self, file_name, root_alias):
  92. for root in root_alias:
  93. # Remove trailing os.sep
  94. root = os.path.expandvars(root)
  95. root = os.path.normpath(root).lower()
  96. if file_name.startswith(root):
  97. base_name = file_name.replace(root.lower(), "")
  98. if base_name[0] == os.sep:
  99. # Get back the original case
  100. file_name = self.view.file_name()
  101. # Remove first os.sep character and get base name
  102. self.base_name = file_name[len(file_name)-len(base_name)+1:]
  103. return True
  104. return False
  105. # Return True if the file is part of an environment
  106. def is_enabled(self):
  107. Prefs.show()
  108. verbose(log="is_enabled()")
  109. file_name = self.view.file_name()
  110. self.initial_env_name = ''
  111. base_name = ''
  112. if file_name is not None and len(file_name) > 0:
  113. file_name = file_name.lower()
  114. verbose(log="file_name: " + file_name)
  115. # Loop into registered environment
  116. for env in Prefs.environment:
  117. for env_name, root_alias in env.items():
  118. # Test if file_name is part of an environment
  119. if self.is_filename_part_of_env(file_name, root_alias):
  120. self.initial_env_name = env_name
  121. return True
  122. sublime.status_message("The current file is not part of an environment")
  123. return False
  124. if int(sublime.version()) < 3000:
  125. plugin_loaded()