コード例 #1
0
def open_file(window: sublime.Window,
              file_path: str,
              flags: int = 0,
              group: int = -1) -> Promise[Optional[sublime.View]]:
    """Open a file asynchronously. It is only safe to call this function from the UI thread."""

    # window.open_file brings the file to focus if it's already opened, which we don't want.
    # So we first check if there's already a view for that file.
    view = window.find_open_file(file_path)
    if view:
        return Promise.resolve(view)

    view = window.open_file(file_path, flags, group)
    if not view.is_loading():
        # It's already loaded. Possibly already open in a tab.
        return Promise.resolve(view)

    # Is the view opening right now? Then return the associated unresolved promise
    for fn, value in opening_files.items():
        if fn == file_path or os.path.samefile(fn, file_path):
            # Return the unresolved promise. A future on_load event will resolve the promise.
            return value[0]

    # Prepare a new promise to be resolved by a future on_load event (see the event listener in main.py)
    def fullfill(resolve: ResolveFunc[Optional[sublime.View]]) -> None:
        global opening_files
        # Save the promise in the first element of the tuple -- except we cannot yet do that here
        opening_files[file_path] = (None, resolve)  # type: ignore

    promise = Promise(fullfill)
    tup = opening_files[file_path]
    # Save the promise in the first element of the tuple so that the for-loop above can return it
    opening_files[file_path] = (promise, tup[1])
    return promise
コード例 #2
0
ファイル: zorgmode.py プロジェクト: rdpli/Zorgmode
def agenda_meta_info_get_or_create_view(window: sublime.Window, meta_info: Agenda.AgendaItemMetaInfo):
    if meta_info.file_name is not None:
        view = window.find_open_file(meta_info.file_name)
        if view is not None:
            return view
        return window.open_file(meta_info.file_name)

    for view in window.views():
        if view.id() == meta_info.view_id:
            return view

    raise ZorgmodeError("Cannot find file for this item")
コード例 #3
0
def get_line(window: sublime.Window, file_name: str, row: int) -> str:
    '''
    Get the line from the buffer if the view is open, else get line from linecache.
    row - is 0 based. If you want to get the first line, you should pass 0.
    '''
    view = window.find_open_file(file_name)
    if view:
        # get from buffer
        point = view.text_point(row, 0)
        return view.substr(view.line(point)).strip()
    else:
        # get from linecache
        # linecache row is not 0 based, so we increment it by 1 to get the correct line.
        return linecache.getline(file_name, row + 1).strip()