Example #1
0
    def text_edit(context, text):
        # in existing line: do not insert whitespace
        if not context.is_cursor_at_end() and text.endswith(" "):
            text = text[:-1]

        start = Position(
            line=context.pos.line,
            character=context.pos.char - len(context.word),
        )
        end = Position(line=context.pos.line, character=context.pos.char)
        return TextEdit(Range(start, end), text)
Example #2
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
Example #3
0
 def to_completion(self, context):
     start = Position(
         line=context.pos.line,
         character=context.pos.char - len(context.word),
     )
     end = Position(
         line=context.pos.line,
         character=context.pos.char,
     )
     return self.completion_build(
         label=self.name(),
         detail=f'Command: {self.description()}',
         text_edit=TextEdit(Range(start, end), f'{self.name()} '),
         documentation=self.description(),
         completion_kind=CompletionItemKind.Unit,
         context=context,
     )
Example #4
0
def test_indent(indentor, ws, story, expected):
    doc = Document(uri=".my.uri.", text=story)
    lines = story.split("\n")
    # select the last pos in the provided story
    pos = Position(line=len(lines) - 1, character=len(lines[-1]))
    assert (
        indentor.indent(ws, doc, pos, indent_unit="  ")["indent"] == expected
    )
Example #5
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 == []
Example #6
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 == []
Example #7
0
 def to_completion(self, context):
     start = Position(
         line=context.pos.line,
         character=context.pos.char - len(context.word),
     )
     end = Position(
         line=context.pos.line,
         character=context.pos.char,
     )
     return self.completion_build(
         label=self.name(),
         detail=self.description(),
         text_edit=TextEdit(Range(start, end), f'{self.name()} '),
         documentation=self._readme,
         documentation_kind=MarkupKind.Markdown,
         completion_kind=CompletionItemKind.Method,
         context=context,
     )
Example #8
0
def test_document_line_to_cursor(text, pos, expected, patch):
    """
    Tests whether the word on the cursor is properly detected
    """
    patch.init(Document)
    patch.object(Document, 'line', return_value=text)
    doc = Document()
    r = doc.line_to_cursor(Position(*pos))
    assert r == expected
Example #9
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")] == []
Example #10
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"]
Example #11
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")] == []
Example #12
0
 def to_completion(self, context):
     """
     Returns a LSP representation.
     """
     start = Position(
         line=context.pos.line,
         character=context.pos.char - len(context.word),
     )
     end = Position(
         line=context.pos.line,
         character=context.pos.char,
     )
     return self.completion_build(
         label=self.name(),
         text_edit=TextEdit(Range(start, end), f'{self.name()}:'),
         detail=f'Arg. {self.type_()}',
         documentation=self.description(),
         completion_kind=CompletionItemKind.Value,
         context=context,
     )
Example #13
0
def test_indent_edits(indentor, ws):
    doc = Document(uri=".my.uri.", text="a = 1")
    pos = Position(line=0, character=5)
    assert indentor.indent(ws, doc, pos, indent_unit="  ") == {
        "indent": "",
        "textEdits": [
            {
                "newText": "\n",
                "range": {
                    "end": {"character": 5, "line": 0},
                    "start": {"character": 5, "line": 0},
                },
            }
        ],
    }
Example #14
0
def test_indent_edits2(indentor, ws):
    doc = Document(uri=".my.uri.", text="\ntry")
    pos = Position(line=1, character=3)
    assert indentor.indent(ws, doc, pos, indent_unit="  ") == {
        "indent": indent_unit,
        "textEdits": [
            {
                "newText": "\n" + indent_unit,
                "range": {
                    "end": {"character": 3, "line": 1},
                    "start": {"character": 3, "line": 1},
                },
            }
        ],
    }
Example #15
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"]
Example #16
0
def test_range():
    assert str(Range(Position(0, 1), Position(0, 2))) == \
            'Range(start=Pos(l=0,c=1),end=Pos(l=0,c=2))'
Example #17
0
File: basic.py Project: jayvdb/sls
 def test(self, pos):
     result = self.get_completion_for(Position(*pos))
     # filter only for label for now
     return [k['label'] for k in result['items']]
Example #18
0
def test_indent_options(indentor, ws):
    doc = Document(uri=".my.uri.", text="  try")
    pos = Position(line=0, character=8)
    assert (
        indentor.indent(ws, doc, pos, indent_unit="    ")["indent"] == "      "
    )
Example #19
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()