Skip to content

Commit

Permalink
Merge 31e4f08 into 7e8e955
Browse files Browse the repository at this point in the history
  • Loading branch information
tomv564 committed Dec 10, 2018
2 parents 7e8e955 + 31e4f08 commit 044a705
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 146 deletions.
93 changes: 0 additions & 93 deletions plugin/core/test_paths.py

This file was deleted.

74 changes: 21 additions & 53 deletions plugin/core/workspace.py
@@ -1,69 +1,37 @@
import os
try:
from typing import List, Optional, Any, Iterable
assert List and Optional and Any and Iterable
from typing import List, Optional, Any
assert List and Optional and Any
except ImportError:
pass

from .logging import debug
from .types import ViewLike


def get_filename_from_view(view: ViewLike) -> 'Optional[str]':
if not view:
debug("No view is active in current window")
return None # https://github.com/tomv564/LSP/issues/219
filename = view.file_name()
if not filename:
debug("Couldn't determine project directory since no folders are open",
"and the current file isn't saved on the disk.")
return filename


def get_directory_name(view: ViewLike) -> 'Optional[str]':
filename = get_filename_from_view(view)
if filename:
project_path = os.path.dirname(filename)
return project_path
return None


def find_path_among_multi_folders(folders: 'Iterable[str]',
view: ViewLike) -> 'Optional[str]':
filename = get_filename_from_view(view)
if not filename:
return None
folders = [os.path.realpath(f) for f in folders]
file = view.file_name()
if not file:
return None
file = os.path.realpath(file)
while file not in folders:
file = os.path.dirname(file)
if os.path.dirname(file) == file:
# We're at the root of the filesystem.
file = None
break
debug('project path is', file)
return file
# from .types import WindowLike


def get_project_path(window: 'Any') -> 'Optional[str]':
"""
Returns the project folder or the parent folder of the active view
Returns the first project folder or the parent folder of the active view
"""
if not window:
return None
num_folders = len(window.folders())
if num_folders == 0:
return get_directory_name(window.active_view())
elif num_folders == 1:
if len(window.folders()):
folder_paths = window.folders()
return folder_paths[0]
else: # num_folders > 1
return find_path_among_multi_folders(
window.folders(),
window.active_view())
else:
view = window.active_view()
if view:
filename = view.file_name()
if filename:
project_path = os.path.dirname(filename)
debug("Couldn't determine project directory since no folders are open!",
"Using", project_path, "as a fallback.")
return project_path
else:
debug("Couldn't determine project directory since no folders are open",
"and the current file isn't saved on the disk.")
return None
else:
debug("No view is active in current window")
return None # https://github.com/tomv564/LSP/issues/219


def get_common_parent(paths: 'List[str]') -> str:
Expand Down

0 comments on commit 044a705

Please sign in to comment.