Example #1
0
    def test_tick(self):
        target = Mock()
        indicator = ActivityIndicator(target)

        results = [
            '[=          ]',
            '[ =         ]',
            '[  =        ]',
            '[   =       ]',
            '[    =      ]',
            '[     =     ]',
            '[      =    ]',
            '[       =   ]',
            '[        =  ]',
            '[         = ]',
            '[          =]',
            '[         = ]',
            '[        =  ]',
            '[       =   ]',
            '[      =    ]',
            '[     =     ]',
            '[    =      ]',
            '[   =       ]',
            '[  =        ]',
            '[ =         ]',
            '[=          ]',
            '[ =         ]',
        ]

        indicator.update()
        for result in results:
            target.set.assert_called_once_with(result)
            target.set.reset_mock()
            indicator.tick()
Example #2
0
def start_indicator(view, msg) -> None:
    global activity_indicator
    if activity_indicator:
        activity_indicator.label = msg
    else:
        activity_indicator = ActivityIndicator(view, msg)
        activity_indicator.start()
Example #3
0
    def _install_or_update(cls) -> None:
        try:
            with ActivityIndicator(
                    target=sublime.active_window(),
                    label="Downloading TAGML language server binary",
            ):
                dest_path = os.path.dirname(cls.binary)
                os.makedirs(dest_path, exist_ok=True)
                urlretrieve(url=SERVER_URL, filename=cls.binary)
                cls.ready = not cls._needs_update_or_installation()
                if not cls.ready:
                    try:
                        os.remove(cls.binary)
                    except FileNotFoundError:
                        pass
                    raise Exception("CRC error!")
                else:
                    # clear old server binaries
                    for file_name in os.listdir(dest_path):
                        if os.path.splitext(file_name)[1].lower() != ".jar":
                            continue

                        try:
                            file_path = os.path.join(dest_path, file_name)
                            if not os.path.samefile(file_path, cls.binary):
                                os.remove(file_path)
                        except FileNotFoundError:
                            pass

        except Exception as error:
            sublime.error_message(
                "Error downloading TAGML server binary!\n\n" + str(error))

        cls.thread = None
Example #4
0
    def test_start_twice_error(self):
        target = Mock()
        indicator = ActivityIndicator(target)

        with indicator:
            with self.assertRaises(ValueError):
                indicator.start()
Example #5
0
            def _download() -> None:
                debug('Downloading server from', url)
                target = sublime.active_window()
                label = 'Downloading PromQL language server'

                with ActivityIndicator(target, label):
                    try:
                        opener = FancyURLopener()
                        tmp_file, _ = opener.retrieve(url)

                        if not checksum_verified(checksum, tmp_file):
                            debug('Checksum error.')
                            sublime.status_message('Server binary',
                                                   os.path.basename(tmp_file),
                                                   'checkusm error.')
                            return

                        # extract and copy the cache
                        with tarfile.open(tmp_file) as tf:
                            tf.extractall(self._cache_path)

                        os.unlink(tmp_file)

                        self._ready = True
                    except Exception as ex:
                        debug('Failed downloading server:', ex)
                    finally:
                        opener.close()
Example #6
0
 def test_tick_called(self):
     target = Mock()
     indicator = ActivityIndicator(target)
     with indicator:
         target.set.assert_called_once_with('[=          ]')
         target.set.reset_mock()
         yield 150
         target.set.assert_called_once_with('[ =         ]')
Example #7
0
 def _start_indicator(self, msg: str = "") -> None:
     if self._activity_indicator:
         self._activity_indicator.label = msg
     else:
         view = sublime.active_window().active_view()
         if view:
             self._activity_indicator = ActivityIndicator(view, msg)
             self._activity_indicator.start()
    def download_server(self) -> None:
        target = sublime.active_window()
        label = "Downloading zip file..."

        with ActivityIndicator(target, label):
            tmp_file, _ = urlretrieve(self._url)
            self.unpack_server(tmp_file)
            os.unlink(tmp_file)
Example #9
0
    def test_contextmanager(self):
        target = Mock()
        indicator = ActivityIndicator(target)

        with indicator:
            target.set.assert_called_once_with('[=          ]')

        target.clear.assert_called_once_with()
Example #10
0
 def perform_install() -> None:
     try:
         message = '{}: Installing server in path: {}'.format(name, cls.storage_path())
         log_and_show_message(message, show_in_status=False)
         with ActivityIndicator(sublime.active_window(), message):
             server.install_or_update()
         log_and_show_message('{}: Server installed. Sublime Text restart is required.'.format(name))
     except Exception as exception:
         log_and_show_message('{}: Server installation error: {}'.format(name, str(exception)))
 def test_tick_called(self):
     target = Mock()
     indicator = ActivityIndicator(target)
     with indicator:
         target.set.assert_called_once_with('[=          ]')
         target.set.reset_mock()
         import time
         time.sleep(0.15)
         target.set.assert_called_once_with('[ =         ]')
Example #12
0
 def install_node(self) -> None:
     os.makedirs(os.path.dirname(self._install_in_progress_marker_file), exist_ok=True)
     open(self._install_in_progress_marker_file, 'a').close()
     with ActivityIndicator(sublime.active_window(), 'Installing Node.js'):
         install_node = InstallNode(self._base_dir, self._node_version)
         install_node.run()
         self.resolve_paths()
     remove(self._install_in_progress_marker_file)
     self.resolve_paths()
Example #13
0
def download_server():
    log_debug("Downloading server from {}".format(SERVER_URL))
    target = sublime.active_window()
    label = "Downloading Elixir language server binary"

    with ActivityIndicator(target, label):
        try:
            tmp_file, _ = urlretrieve(SERVER_URL)
            unpack_server(tmp_file)
            os.unlink(tmp_file)
        except Exception as ex:
            log_debug("Failed downloading server: {}".format(ex))
Example #14
0
    def test_start_stop(self):
        target = Mock()
        indicator = ActivityIndicator(target)

        indicator.start()
        target.set.assert_called_once_with('[=          ]')
        target.set.reset_mock()

        indicator.stop()
        target.clear.assert_called_once_with()

        indicator.start()
        target.set.assert_called_once_with('[=          ]')

        indicator.stop()
Example #15
0
    def run(self, edit, only_selection=True):
        settings = sublime.load_settings(settings_filename())
        binary_path = settings.get(PREF_CLANG_FORMAT_PATH)
        if not binary_path:
            binary_path = which(binary_name())
            if not binary_path or not is_exe(binary_path):
                sublime.message_dialog(MISSING_BINARY_MESSAGE % binary_name())
                return

        args = [binary_path, '-fallback-style', style]
        if self.view.file_name():
            args.extend(['-assume-filename', self.view.file_name()])
        else:
            print(
                'Checking style without knowing file type. Results might be innacurate!'
            )

        if only_selection:
            for region in self.view.sel():
                region_offset = min(region.a, region.b)
                region_length = abs(region.b - region.a)
                args.extend([
                    '-offset',
                    str(region_offset), '-length',
                    str(region_length)
                ])

        buffer_text = self.view.substr(sublime.Region(0, self.view.size()))
        encoding = self.view.encoding()
        encoding = encoding if encoding != 'Undefined' else 'utf-8'
        stdin = buffer_text.encode(encoding)
        viewport_pos = self.view.viewport_position()
        # Show progress indicator if formatting takes longer than 1s.
        self._indicator = ActivityIndicator(self.view,
                                            'ClangFormat: Formatting...')
        sublime.set_timeout(self.start_indicator, 1000)

        start_thread(
            lambda output: self.on_formatting_success(
                viewport_pos, output, encoding), self.on_formatting_error,
            args, stdin)
    def _install_dependencies(self, cache_server_path):
        dependencies_installed = os.path.isdir(os.path.join(cache_server_path, 'node_modules'))

        if dependencies_installed:
            self._is_ready = True
            return

        # this will be called only when the plugin gets:
        # - installed for the first time,
        # - or when updated on package control
        install_message = '{}: Installing server'.format(self._package_name)
        log_and_show_message(install_message, show_in_status=False)

        active_window = sublime.active_window()
        if active_window:
            self._activity_indicator = ActivityIndicator(active_window.active_view(), install_message)
            self._activity_indicator.start()

        run_command(
            self._on_install_success, self._on_install_error,
            ["npm", "install", "--verbose", "--production", "--prefix", cache_server_path, cache_server_path]
        )
Example #17
0
    def test_label(self):
        target = Mock()
        indicator = ActivityIndicator(target, 'Hello, World!')

        with indicator:
            target.set.assert_called_once_with('Hello, World! [=          ]')
Example #18
0
 def test_init_with_window(self):
     window = Window(0)
     indicator = ActivityIndicator(window)
     self.assertIsInstance(indicator._target, WindowTarget)
Example #19
0
 def test_init_with_view(self):
     view = View(0)
     indicator = ActivityIndicator(view)
     self.assertIsInstance(indicator._target, ViewTarget)