Beispiel #1
0
def build_cache(text, line):
    doc = Document(uri=".text.", text=text)
    pos = Position(line, 0)
    context = CompletionContext(ws=None, doc=doc, pos=pos)
    cache = ContextCache(hub=hub)
    cache.update(context)
    return cache
Beispiel #2
0
def test_error(magic):
    doc = Document(uri=".text.", text="a = $\n  b = \n")
    pos = Position(1, 4)
    context = CompletionContext(ws=magic(), doc=doc, pos=pos)
    glob = GlobalScopeCache(story_hub=hub)
    glob.update(context)
    fns = [*glob.function_table.functions.keys()]
    assert fns == []
Beispiel #3
0
def test_error(magic):
    doc = Document(uri=".text.", text="a = $\n  b = \n")
    pos = Position(1, 4)
    context = CompletionContext(ws=magic(), doc=doc, pos=pos)
    global_ = magic()
    current = CurrentScopeCache(global_=global_, hub=hub)
    current.update(context)
    symbols = [s.name() for s in current.current_scope.symbols()]
    assert symbols == []
Beispiel #4
0
def test_complete(magic):
    doc = Document(uri=".text.", text="a = $")
    pos = Position(0, 0)
    context = CompletionContext(ws=magic(), doc=doc, pos=pos)
    global_ = magic()
    global_.global_scope = Scope.root()
    current = CurrentScopeCache(global_=global_, hub=hub)
    current.update(context)
    assert [x.symbol.name() for x in current.complete("a")] == ["app"]
    assert [x.symbol.name() for x in current.complete("b")] == []
Beispiel #5
0
def test_complete(magic):
    doc = Document(uri=".text.", text="function foo\n  a = 1\n\nb = 0")
    pos = Position(3, 0)
    context = CompletionContext(ws=magic(), doc=doc, pos=pos)
    global_ = magic()
    global_.global_scope = Scope.root()
    glob = GlobalScopeCache(story_hub=hub)
    glob.update(context)
    assert [x.function.name() for x in glob.complete("f")] == ["foo"]
    assert [x.function.name() for x in glob.complete("b")] == []
Beispiel #6
0
def test_caching(magic):
    doc = Document(uri=".text.", text="function foo\n  a = 1\n\nb = 0")
    pos = Position(3, 0)
    context = CompletionContext(ws=magic(), doc=doc, pos=pos)
    global_ = magic()
    global_.global_scope = Scope.root()
    glob = GlobalScopeCache(story_hub=hub)
    glob.update(context)
    fns = [*glob.function_table.functions.keys()]
    assert fns == ["foo"]
Beispiel #7
0
def test_caching(magic):
    doc = Document(uri=".text.", text="a = $")
    pos = Position(0, 0)
    context = CompletionContext(ws=magic(), doc=doc, pos=pos)
    global_ = magic()
    global_.global_scope = Scope.root()
    current = CurrentScopeCache(global_=global_, hub=hub)
    current.update(context)
    symbols = [s.name() for s in current.current_scope.symbols()]
    assert symbols == ["app"]

    # test caching
    current.update(context)
    symbols = [s.name() for s in current.current_scope.symbols()]
    assert symbols == ["app"]
Beispiel #8
0
def test_context(magic):
    doc = magic()
    ws = 12
    pos = 42

    context = CompletionContext(ws, doc, pos)

    doc.word_on_cursor.assert_called_with(pos)
    doc.line_to_cursor.assert_called_with(pos)

    assert context.ws == ws
    assert context.doc == doc
    assert context.pos == pos
    assert context.word == doc.word_on_cursor()
    assert context.line == doc.line_to_cursor()
Beispiel #9
0
def test_complete_plugin(magic, patch):
    patch.init(Document)
    patch.many(Document, ['line_to_cursor', 'word_on_cursor'])
    my_plugin = magic()
    i1 = magic()
    i2 = magic()
    my_plugin.complete.return_value = [i1, i2]
    c = Completion(plugins=[my_plugin])
    doc = Document()
    ws = magic()
    pos = magic()
    result = c.complete(ws, doc, pos)
    my_plugin.complete.call_args == (CompletionContext(ws, doc, pos))
    assert result == {
        'isIncomplete': False,
        'items': [i1.to_completion(), i2.to_completion()],
    }
Beispiel #10
0
    def indent(self, ws, doc, pos, indent_unit):
        """
        Returns the indentation for the next line based for the given document
        and position.
        """
        context = CompletionContext(ws=ws, doc=doc, pos=pos)
        log.info("Line on cursor: %s", context.line)
        indent_state = IndentState.detect(context.line, indent_unit)

        self._indent(context, indent_state)

        indentation = indent_state.indentation()
        insert_pos = pos
        return {
            "indent": indentation,
            "textEdits": [
                TextEdit(
                    Range(start=insert_pos, end=insert_pos),
                    new_text=f"\n{indentation}",
                ).dump()
            ],
        }
Beispiel #11
0
def test_context_is_cursor_not_at_end(magic):
    doc = magic()
    doc.line.return_value = "abc"
    context = CompletionContext(ws=None, doc=doc, pos=Position(0, 2))
    assert not context.is_cursor_at_end()