Skip to content

Commit

Permalink
Move functions for dealing with ST to their own module
Browse files Browse the repository at this point in the history
  • Loading branch information
ig0774 committed Feb 8, 2016
1 parent b8c7fe7 commit 629093d
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 111 deletions.
114 changes: 3 additions & 111 deletions getTeXRoot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,119 +6,14 @@
# we are on ST2 and Python 2.X
_ST3 = False
from latextools_utils.is_tex_file import get_tex_extensions
from latextools_utils.sublime import get_project_file_name
else:
_ST3 = True
from .latextools_utils.is_tex_file import get_tex_extensions

from .latextools_utils.sublime import get_project_file_name

import os.path
import re
import json


# normalizes the paths stored in sublime session files on Windows
# from:
# /c/path/to/file.ext
# to:
# c:\path\to\file.ext
def normalize_sublime_path(path):
if sublime.platform() == 'windows':
return os.path.normpath(
path.lstrip('/').replace('/', ':/', 1)
)
else:
return path


# long, complex hack for ST2 to load the project file from the current session
def get_project_file_name(view):
window_id = view.window().id()
if window_id is None:
return None

session = os.path.normpath(
os.path.join(
sublime.packages_path(),
'..',
'Settings',
'Session.sublime_session'
)
)

auto_save_session = os.path.normpath(
os.path.join(
sublime.packages_path(),
'..',
'Settings',
'Auto Save Session.sublime_session'
)
)

session = auto_save_session if os.path.exists(auto_save_session) else session

if not os.path.exists(session):
return None

project_file = None

# we tell that we have found the current project's project file by
# looking at the folders registered for that project and comparing it
# to the open directorys in the current window
found_all_folders = False
try:
with open(session, 'r') as f:
session_data = f.read().replace('\t', ' ')
j = json.loads(session_data, strict=False)
projects = j.get('workspaces', {}).get('recent_workspaces', [])

for project_file in projects:
found_all_folders = True

project_file = normalize_sublime_path(project_file)
try:
with open(project_file, 'r') as fd:
project_json = json.loads(fd.read(), strict=False)

if 'folders' in project_json:
project_folders = project_json['folders']
for directory in view.window().folders():
found = False
for folder in project_folders:
folder_path = normalize_sublime_path(folder['path'])
# handle relative folder paths
if not os.path.isabs(folder_path):
folder_path = os.path.normpath(
os.path.join(os.path.dirname(project_file), folder_path)
)

if folder_path == directory:
found = True
break

if not found:
found_all_folders = False
break

if found_all_folders:
break
except:
found_all_folders = False
except:
pass

if not found_all_folders:
project_file = None

if (
project_file is None or
not project_file.endswith('.sublime-project') or
not os.path.exists(project_file)
):
return None

print('Using project file: %s' % project_file)
return project_file


# Parse magic comments to retrieve TEX root
# Stops searching for magic comments at first non-comment line of file
Expand Down Expand Up @@ -177,10 +72,7 @@ def get_tex_root_from_settings(view):
if os.path.isfile(root):
return root
else:
try:
proj_file = view.window().project_file_name()
except AttributeError:
proj_file = get_project_file_name(view)
proj_file = get_project_file_name(view)

if proj_file:
project_dir = os.path.dirname(proj_file)
Expand Down
2 changes: 2 additions & 0 deletions latextools_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@

try:
from latextools_utils.settings import get_setting
from latextools_utils import sublime
except ImportError:
from .settings import get_setting
from . import sublime
117 changes: 117 additions & 0 deletions latextools_utils/sublime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import json
import os.path
import sublime


__all__ = ['normalize_path', 'get_project_file_name']


# normalizes the paths stored in sublime session files on Windows
# from:
# /c/path/to/file.ext
# to:
# c:\path\to\file.ext
def normalize_path(path):
if sublime.platform() == 'windows':
return os.path.normpath(
path.lstrip('/').replace('/', ':/', 1)
)
else:
return path


def get_project_file_name(view):
try:
return view.window().project_file_name()
except AttributeError:
return _get_project_file_name(view)


# long, complex hack for ST2 to load the project file from the current session
def _get_project_file_name(view):
window_id = view.window().id()
if window_id is None:
return None

session = os.path.normpath(
os.path.join(
sublime.packages_path(),
'..',
'Settings',
'Session.sublime_session'
)
)

auto_save_session = os.path.normpath(
os.path.join(
sublime.packages_path(),
'..',
'Settings',
'Auto Save Session.sublime_session'
)
)

session = auto_save_session if os.path.exists(auto_save_session) else session

if not os.path.exists(session):
return None

project_file = None

# we tell that we have found the current project's project file by
# looking at the folders registered for that project and comparing it
# to the open directorys in the current window
found_all_folders = False
try:
with open(session, 'r') as f:
session_data = f.read().replace('\t', ' ')
j = json.loads(session_data, strict=False)
projects = j.get('workspaces', {}).get('recent_workspaces', [])

for project_file in projects:
found_all_folders = True

project_file = normalize_path(project_file)
try:
with open(project_file, 'r') as fd:
project_json = json.loads(fd.read(), strict=False)

if 'folders' in project_json:
project_folders = project_json['folders']
for directory in view.window().folders():
found = False
for folder in project_folders:
folder_path = normalize_path(folder['path'])
# handle relative folder paths
if not os.path.isabs(folder_path):
folder_path = os.path.normpath(
os.path.join(os.path.dirname(project_file), folder_path)
)

if folder_path == directory:
found = True
break

if not found:
found_all_folders = False
break

if found_all_folders:
break
except:
found_all_folders = False
except:
pass

if not found_all_folders:
project_file = None

if (
project_file is None or
not project_file.endswith('.sublime-project') or
not os.path.exists(project_file)
):
return None

print('Using project file: %s' % project_file)
return project_file

0 comments on commit 629093d

Please sign in to comment.