Example #1
0
 def startsHelper(self, s, i, kind, tags, tag=None):
     '''return True if s[i:] starts section.
     Sets sigStart, sigEnd, sigId and codeEnd ivars.'''
     trace = False
     self.codeEnd = self.sigEnd = self.sigId = None
     self.sigStart = i
     sigStart = i
     ok, sigId, i = self.isSectionLine(s, i)
     if not sigId or not ok:
         # if trace: g.trace('fail',repr(g.getLine(s,i)))
         return False
     i = sigEnd = g.skip_line(s, i)
     # Skip everything until the next section.
     while i < len(s):
         progress = i
         ok, junk, junk = self.isSectionLine(s, i)
         if ok: break # don't change i.
         i = g.skip_line(s, i)
         assert progress < i
     # Success: set the ivars.
     self.sigStart = sigStart
     self.codeEnd = i
     self.sigEnd = sigEnd
     self.sigId = sigId
     self.classId = None
     # Note: backing up here is safe because
     # we won't back up past scan's 'start' point.
     # Thus, characters will never be output twice.
     k = self.sigStart
     if not g.match(s, k, '\n'):
         self.sigStart = g.find_line_start(s, k)
     if trace: g.trace(sigId, 'returns\n' + s[self.sigStart: i] + '\nEND')
     return True
Example #2
0
    def startsSection(self, s, i):
        '''Scan a line and possible one or two other lines,
        looking for an underlined or overlined/underlined name.

        Return (kind,name,i):
            kind: in ('under','over','plain')
            name: the name of the underlined or overlined line.
            i: the following character if kind is not 'plain'
            ch: the underlining and possibly overlining character.
        '''
        trace = False and not g.unitTesting
        verbose = False
        # Under/overlines can not begin with whitespace.
        i1, j, nows, line = self.getLine(s, i)
        ch, kind = '', 'plain' # defaults.
        if nows and self.isUnderLine(line): # an overline.
            name_i = g.skip_line(s, i1)
            name_i, name_j = g.getLine(s, name_i)
            name = s[name_i: name_j].strip()
            next_i = g.skip_line(s, name_i)
            i, j, nows, line2 = self.getLine(s, next_i)
            n1, n2, n3 = len(line), len(name), len(line2)
            ch1, ch3 = line[0], line2 and line2[0]
            ok = (nows and self.isUnderLine(line2) and
                n1 >= n2 and n2 > 0 and n3 >= n2 and ch1 == ch3)
            if ok:
                i += n3
                ch, kind = ch1, 'over'
                if ch1 not in self.underlines2:
                    self.underlines2.append(ch1)
                    if trace: g.trace('*** underlines2', self.underlines2, name)
                if trace and verbose:
                    g.trace('\nline  %s\nname  %s\nline2 %s' % (
                        repr(line), repr(name), repr(line2))) #,'\n',g.callers(4))
        else:
            name = line.strip()
            i = g.skip_line(s, i1)
            i, j, nows2, line2 = self.getLine(s, i)
            n1, n2 = len(name), len(line2)
            # look ahead two lines.
            i3, j3 = g.getLine(s, j)
            name2 = s[i3: j3].strip()
            i4, j4, nows4, line4 = self.getLine(s, j3)
            n3, n4 = len(name2), len(line4)
            overline = (
                nows2 and self.isUnderLine(line2) and
                nows4 and self.isUnderLine(line4) and
                n3 > 0 and n2 >= n3 and n4 >= n3)
            ok = (not overline and nows2 and self.isUnderLine(line2) and
                n1 > 0 and n2 >= n1)
            if ok:
                i += n2
                ch, kind = line2[0], 'under'
                if ch not in self.underlines1:
                    self.underlines1.append(ch)
                    if trace: g.trace('*** underlines1', self.underlines1, name)
                if trace and verbose: g.trace('\nname  %s\nline2 %s' % (
                    repr(name), repr(line2)))
        return kind, name, i, ch
Example #3
0
    def startsSection(self, s, i):
        '''Scan a line and possible one or two other lines,
        looking for an underlined or overlined/underlined name.

        Return (kind,name,i):
            kind: in ('under','over','plain')
            name: the name of the underlined or overlined line.
            i: the following character if kind is not 'plain'
            ch: the underlining and possibly overlining character.
        '''
        trace = False and not g.unitTesting
        verbose = False
        # Under/overlines can not begin with whitespace.
        i1, j, nows, line = self.getLine(s, i)
        ch, kind = '', 'plain' # defaults.
        if nows and self.isUnderLine(line): # an overline.
            name_i = g.skip_line(s, i1)
            name_i, name_j = g.getLine(s, name_i)
            name = s[name_i: name_j].strip()
            next_i = g.skip_line(s, name_i)
            i, j, nows, line2 = self.getLine(s, next_i)
            n1, n2, n3 = len(line), len(name), len(line2)
            ch1, ch3 = line[0], line2 and line2[0]
            ok = (nows and self.isUnderLine(line2) and
                n1 >= n2 and n2 > 0 and n3 >= n2 and ch1 == ch3)
            if ok:
                i += n3
                ch, kind = ch1, 'over'
                if ch1 not in self.underlines2:
                    self.underlines2.append(ch1)
                    if trace: g.trace('*** underlines2', self.underlines2, name)
                if trace and verbose:
                    g.trace('\nline  %s\nname  %s\nline2 %s' % (
                        repr(line), repr(name), repr(line2))) #,'\n',g.callers(4))
        else:
            name = line.strip()
            i = g.skip_line(s, i1)
            i, j, nows2, line2 = self.getLine(s, i)
            n1, n2 = len(name), len(line2)
            # look ahead two lines.
            i3, j3 = g.getLine(s, j)
            name2 = s[i3: j3].strip()
            i4, j4, nows4, line4 = self.getLine(s, j3)
            n3, n4 = len(name2), len(line4)
            overline = (
                nows2 and self.isUnderLine(line2) and
                nows4 and self.isUnderLine(line4) and
                n3 > 0 and n2 >= n3 and n4 >= n3)
            ok = (not overline and nows2 and self.isUnderLine(line2) and
                n1 > 0 and n2 >= n1)
            if ok:
                i += n2
                ch, kind = line2[0], 'under'
                if ch not in self.underlines1:
                    self.underlines1.append(ch)
                    if trace: g.trace('*** underlines1', self.underlines1, name)
                if trace and verbose: g.trace('\nname  %s\nline2 %s' % (
                    repr(name), repr(line2)))
        return kind, name, i, ch
Example #4
0
def line_to_headline(self, event=None):
    '''
    Create child node from the selected line.
    
    Cut the selected line and make it the new node's headline
    '''
    c, w = self, self.frame.body.wrapper
    p = c.p
    ins, s = w.getInsertPoint(), p.b
    u, undoType = c.undoer, 'Extract Line'
    i = g.find_line_start(s, ins)
    j = g.skip_line(s, i)
    line = s[i:j].strip()
    if not line:
        return
    u.beforeChangeGroup(p, undoType)
    undoData = u.beforeInsertNode(p)
    p2 = p.insertAsLastChild()
    p2.h = line
    u.afterInsertNode(p2, undoType, undoData)
    oldText = p.b
    p.b = s[:i] + s[j:]
    w.setInsertPoint(i)
    u.setUndoTypingParams(p, undoType, oldText=oldText, newText=p.b)
    p2.setDirty()
    c.setChanged(True)
    u.afterChangeGroup(p, undoType=undoType)
    c.redraw_after_icons_changed()
    p.expand()
    c.redraw(p)
    c.bodyWantsFocus()
Example #5
0
 def scanHelper(self,s,i,end,parent,kind):
     '''
     Create Leo nodes for all org-mode lines.
     Overrides BaseScanner.scanHelper.
     '''
     assert kind == 'outer' and end == len(s)
     putRef = False
     level = 1 # The root has level 0.
     while i < len(s):
         progress = i
         # Get the next line, with k the index of the last char.
         k = g.skip_line(s,i)
         line = s[i:k]
         if line.startswith('*'):
             # Handle the headline & reset the level.
             j = 0
             while j < len(line) and line[j] == '*':
                 j += 1
             level = j
             putRef = True
             # Cut back the stack, then allocate a new (placeholder) node.
             self.parents = self.parents[:level]
             p = self.findParent(level)
             # Set the headline of the placeholder node.
             p.h = line[j:k].strip()
         else:
             # Append the line to the body.
             p = self.findParent(level)
             p.b = p.b + line
         # Move to the next line.
         i = k
         assert progress < i,'i: %s %s' % (i,repr(line))
     return len(s),putRef,0 # bodyIndent not used.
Example #6
0
 def scanHelper(self, s, i, end, parent, kind):
     '''
     Create Leo nodes for all org-mode lines.
     Overrides BaseScanner.scanHelper.
     '''
     assert kind == 'outer' and end == len(s)
     putRef = False
     level = 1 # The root has level 0.
     while i < len(s):
         progress = i
         # Get the next line, with k the index of the last char.
         k = g.skip_line(s, i)
         line = s[i: k]
         if line.startswith('*'):
             # Handle the headline & reset the level.
             j = 0
             while j < len(line) and line[j] == '*':
                 j += 1
             level = j
             putRef = True
             # Cut back the stack, then allocate a new (placeholder) node.
             self.parents = self.parents[: level]
             p = self.findParent(level)
             # Set the headline of the placeholder node.
             p.h = line[j: k].strip()
         else:
             # Append the line to the body.
             p = self.findParent(level)
             p.b = p.b + line
         # Move to the next line.
         i = k
         assert progress < i, 'i: %s %s' % (i, repr(line))
     return len(s), putRef, 0 # bodyIndent not used.
Example #7
0
 def tokenize(self, s):
     '''Tokenize comments, strings, identifiers, whitespace and operators.'''
     i, result = 0, []
     while i < len(s):
         # Loop invariant: at end: j > i and s[i:j] is the new token.
         j = i
         ch = s[i]
         if ch in '@\n':  # Make *sure* these are separate tokens.
             j += 1
         elif ch == '#':  # Preprocessor directive.
             j = g.skip_to_end_of_line(s, i)
         elif ch in ' \t':
             j = g.skip_ws(s, i)
         elif ch.isalpha() or ch == '_':
             j = g.skip_c_id(s, i)
         elif g.match(s, i, '//'):
             j = g.skip_line(s, i)
         elif g.match(s, i, '/*'):
             j = self.skip_block_comment(s, i)
         elif ch in "'\"":
             j = g.skip_string(s, i)
         else:
             j += 1
         assert j > i
         result.append(''.join(s[i:j]))
         i = j  # Advance.
     return result
Example #8
0
def line_to_headline(self, event=None):
    '''
    Create child node from the selected line.
    
    Cut the selected line and make it the new node's headline
    '''
    c, w = self, self.frame.body.wrapper
    p = c.p
    ins, s = w.getInsertPoint(), p.b
    u, undoType = c.undoer, 'Extract Line'
    i = g.find_line_start(s, ins)
    j = g.skip_line(s, i)
    line = s[i:j].strip()
    if not line:
        return
    u.beforeChangeGroup(p, undoType)
    undoData = u.beforeInsertNode(p)
    p2 = p.insertAsLastChild()
    p2.h = line
    u.afterInsertNode(p2, undoType, undoData)
    oldText = p.b
    p.b = s[:i] + s[j:]
    w.setInsertPoint(i)
    u.setUndoTypingParams(p, undoType, oldText=oldText, newText=p.b)
    p2.setDirty()
    c.setChanged(True)
    u.afterChangeGroup(p, undoType=undoType)
    c.redraw_after_icons_changed()
    p.expand()
    c.redraw(p)
    c.bodyWantsFocus()
Example #9
0
 def tokenize(self, s):
     '''Tokenize comments, strings, identifiers, whitespace and operators.'''
     i, result = 0, []
     while i < len(s):
         # Loop invariant: at end: j > i and s[i:j] is the new token.
         j = i
         ch = s[i]
         if ch in '@\n': # Make *sure* these are separate tokens.
             j += 1
         elif ch == '#': # Preprocessor directive.
             j = g.skip_to_end_of_line(s, i)
         elif ch in ' \t':
             j = g.skip_ws(s, i)
         elif ch.isalpha() or ch == '_':
             j = g.skip_c_id(s, i)
         elif g.match(s, i, '//'):
             j = g.skip_line(s, i)
         elif g.match(s, i, '/*'):
             j = self.skip_block_comment(s, i)
         elif ch in "'\"":
             j = g.skip_string(s, i)
         else:
             j += 1
         assert j > i
         result.append(''.join(s[i: j]))
         i = j # Advance.
     return result
Example #10
0
 def skipSigTail(self, s, i, kind):
     """Skip from the end of the arg list to the start of the block."""
     trace = False and self.trace
     # Pascal interface has no tail.
     if kind == "class":
         return i, True
     start = i
     i = g.skip_ws(s, i)
     for z in self.sigFailTokens:
         if g.match(s, i, z):
             if trace:
                 g.trace("failToken", z, "line", g.skip_line(s, i))
             return i, False
     while i < len(s):
         if self.startsComment(s, i):
             i = self.skipComment(s, i)
         elif g.match(s, i, self.blockDelim1):
             if trace:
                 g.trace(repr(s[start:i]))
             return i, True
         else:
             i += 1
     if trace:
         g.trace("no block delim")
     return i, False
Example #11
0
 def vim_o(self):
     '''Open a new line below the current line N times.'''
     vc = self
     w = vc.event.w
     s = w.getAllText()
     i = w.getInsertPoint()
     i = g.skip_line(s, i)
     w.insert(i, '\n')
     return vc.begin_insert_mode()
Example #12
0
 def vim_o(self):
     '''Open a new line below the current line N times.'''
     vc = self
     w = vc.event.w
     s = w.getAllText()
     i = w.getInsertPoint()
     i = g.skip_line(s,i)
     w.insert(i,'\n')
     return vc.begin_insert_mode()
Example #13
0
    def skip_heredoc_string(self, s, i):
        #@+<< skip_heredoc docstrig >>
        #@+node:ekr.20161130044051.2: *4* << skip_heredoc docstrig >>
        #@@nocolor-node
        '''
        08-SEP-2002 DTHEIN:  added function skip_heredoc_string
        A heredoc string in PHP looks like:

          <<<EOS
          This is my string.
          It is mine. I own it.
          No one else has it.
          EOS

        It begins with <<< plus a token (naming same as PHP variable names).
        It ends with the token on a line by itself (must start in first position.
        '''
        #@-<< skip_heredoc docstrig >>
        j = i
        assert(g.match(s, i, "<<<"))
        # pylint: disable=anomalous-backslash-in-string
        m = re.match("\<\<\<([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)", s[i:])
        if m is None:
            i += 3
            return i
        # 14-SEP-2002 DTHEIN: needed to add \n to find word, not just string
        delim = m.group(1) + '\n'
        i = g.skip_line(s, i) # 14-SEP-2002 DTHEIN: look after \n, not before
        n = len(s)
        while i < n and not g.match(s, i, delim):
            i = g.skip_line(s, i) # 14-SEP-2002 DTHEIN: move past \n
        if i >= n:
            g.scanError("Run on string: " + s[j: i])
        elif g.match(s, i, delim):
            i += len(delim)
        return i
Example #14
0
    def skip_heredoc_string(self, s, i):
        #@+<< skip_heredoc docstrig >>
        #@+node:ekr.20161130044051.2: *4* << skip_heredoc docstrig >>
        #@@nocolor-node
        '''
        08-SEP-2002 DTHEIN:  added function skip_heredoc_string
        A heredoc string in PHP looks like:

          <<<EOS
          This is my string.
          It is mine. I own it.
          No one else has it.
          EOS

        It begins with <<< plus a token (naming same as PHP variable names).
        It ends with the token on a line by itself (must start in first position.
        '''
        #@-<< skip_heredoc docstrig >>
        j = i
        assert (g.match(s, i, "<<<"))
        # pylint: disable=anomalous-backslash-in-string
        m = re.match("\<\<\<([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)", s[i:])
        if m is None:
            i += 3
            return i
        # 14-SEP-2002 DTHEIN: needed to add \n to find word, not just string
        delim = m.group(1) + '\n'
        i = g.skip_line(s, i)  # 14-SEP-2002 DTHEIN: look after \n, not before
        n = len(s)
        while i < n and not g.match(s, i, delim):
            i = g.skip_line(s, i)  # 14-SEP-2002 DTHEIN: move past \n
        if i >= n:
            g.scanError("Run on string: " + s[j:i])
        elif g.match(s, i, delim):
            i += len(delim)
        return i
Example #15
0
 def skipSigTail(self, s, i, kind=None):
     '''Skip from the end of the arg list to the start of the block.'''
     trace = False and self.trace
     i1 = i
     i = self.skipWs(s, i)
     for z in self.sigFailTokens:
         if g.match(s, i, z):
             if trace: g.trace('failToken', z, 'line', g.skip_line(s, i))
             return i, False
     while i < len(s):
         progress = i
         if self.startsComment(s, i):
             i = self.skipComment(s, i)
         elif g.match(s, i, self.blockDelim1):
             if trace: g.trace(repr(s[i1: i]))
             return i, True
         else:
             i += 1
         assert progress < i
     if trace: g.trace('no block delim')
     return i, False
Example #16
0
 def skipSigTail(self, s, i, kind=None):
     '''Skip from the end of the arg list to the start of the block.'''
     trace = False and self.trace
     i1 = i
     i = self.skipWs(s, i)
     for z in self.sigFailTokens:
         if g.match(s, i, z):
             if trace: g.trace('failToken', z, 'line', g.skip_line(s, i))
             return i, False
     while i < len(s):
         progress = i
         if self.startsComment(s, i):
             i = self.skipComment(s, i)
         elif g.match(s, i, self.blockDelim1):
             if trace: g.trace(repr(s[i1:i]))
             return i, True
         else:
             i += 1
         assert progress < i
     if trace: g.trace('no block delim')
     return i, False
Example #17
0
 def skipSigTail(self, s, i, kind):
     '''Skip from the end of the arg list to the start of the block.'''
     trace = False and self.trace
     # Pascal interface has no tail.
     if kind == 'class':
         return i, True
     start = i
     i = g.skip_ws(s, i)
     for z in self.sigFailTokens:
         if g.match(s, i, z):
             if trace: g.trace('failToken', z, 'line', g.skip_line(s, i))
             return i, False
     while i < len(s):
         if self.startsComment(s, i):
             i = self.skipComment(s, i)
         elif g.match(s, i, self.blockDelim1):
             if trace: g.trace(repr(s[start:i]))
             return i, True
         else:
             i += 1
     if trace: g.trace('no block delim')
     return i, False
Example #18
0
def line_to_headline(self, event=None):
    """
    Create child node from the selected line.

    Cut the selected line and make it the new node's headline
    """
    c, p, u, w = self, self.p, self.undoer, self.frame.body.wrapper
    undoType = 'line-to-headline'
    ins, s = w.getInsertPoint(), p.b
    i = g.find_line_start(s, ins)
    j = g.skip_line(s, i)
    line = s[i:j].strip()
    if not line:
        return
    u.beforeChangeGroup(p, undoType)
    #
    # Start outer undo.
    undoData = u.beforeInsertNode(p)
    p2 = p.insertAsLastChild()
    p2.h = line
    u.afterInsertNode(p2, undoType, undoData)
    #
    # "before" snapshot.
    bunch = u.beforeChangeBody(p)
    p.b = s[:i] + s[j:]
    w.setInsertPoint(i)
    p2.setDirty()
    c.setChanged()
    #
    # "after" snapshot.
    u.afterChangeBody(p, undoType, bunch)
    #
    # Finish outer undo.
    u.afterChangeGroup(p, undoType=undoType)
    c.redraw_after_icons_changed()
    p.expand()
    c.redraw(p)
    c.bodyWantsFocus()
Example #19
0
    def scanHelper(self,s,i,end,parent,kind):
        '''Create Leo nodes for all vimoutliner lines.'''
        assert kind == 'outer' and end == len(s)
        while i < len(s):
            # Set k to the end of the line.
            progress = i
            k = g.skip_line(s,i)
            line = s[i:k] # For traces.
            # Skip leading hard tabs, ignore blanks & compute the line's level.
            level = 1 # The root has level 0.
            while i < len(s) and s[i].isspace():
                if s[i] == '\t': level += 1
                i += 1
            if i == k:
                g.trace('ignoring blank line: %s' % (repr(line)))
            elif i < len(s) and s[i] == ':':
                # Append the line to the body.
                i += 1
                if i < len(s) and s[i] == ' ':
                    i += 1
                else:
                    g.trace('missing space after colon: %s' % (repr(line)))
                p = self.findParent(level)
                p.b = p.b + s[i:k]
            else:
                putRef = True
                # Cut back the stack, then allocate a new (placeholder) node.
                self.parents = self.parents[:level]
                p = self.findParent(level)

                # Set the headline of the placeholder node.
                h = s[i:k]
                p.h = h[:-1] if h.endswith('\n') else h
            # Move to the next line.
            i = k
            assert progress < i,'i: %s %s' % (i,repr(line))

        return len(s),putRef,0 # bodyIndent not used.
Example #20
0
 def scanHelper(self, s, i, end, parent, kind):
     '''Create Leo nodes for all vimoutliner lines.'''
     assert kind == 'outer' and end == len(s)
     while i < len(s):
         # Set k to the end of the line.
         progress = i
         k = g.skip_line(s, i)
         line = s[i:k]  # For traces.
         # Skip leading hard tabs, ignore blanks & compute the line's level.
         level = 1  # The root has level 0.
         while i < len(s) and s[i].isspace():
             if s[i] == '\t': level += 1
             i += 1
         if i == k:
             g.trace('ignoring blank line: %s' % (repr(line)))
         elif i < len(s) and s[i] == ':':
             # Append the line to the body.
             i += 1
             if i < len(s) and s[i] == ' ':
                 i += 1
             else:
                 g.trace('missing space after colon: %s' % (repr(line)))
             p = self.findParent(level)
             p.b = p.b + s[i:k]
         else:
             putRef = True
             # Cut back the stack, then allocate a new (placeholder) node.
             self.parents = self.parents[:level]
             p = self.findParent(level)
             # Set the headline of the placeholder node.
             h = s[i:k]
             p.h = h[:-1] if h.endswith('\n') else h
         # Move to the next line.
         i = k
         assert progress < i, 'i: %s %s' % (i, repr(line))
     return len(s), putRef, 0  # bodyIndent not used.