def addComments(self, event=None): #@+<< addComments docstring >> #@+node:ekr.20171123135625.35: *3* << addComments docstring >> #@@pagewidth 50 ''' Converts all selected lines to comment lines using the comment delimiters given by the applicable @language directive. Inserts single-line comments if possible; inserts block comments for languages like html that lack single-line comments. @bool indent_added_comments If True (the default), inserts opening comment delimiters just before the first non-whitespace character of each line. Otherwise, inserts opening comment delimiters at the start of each line. *See also*: delete-comments. ''' #@-<< addComments docstring >> c = self p = c.p head, lines, tail, oldSel, oldYview = self.getBodyLines() if not lines: g.warning('no text selected') return # The default language in effect at p. language = c.frame.body.colorizer.scanLanguageDirectives(p) if c.hasAmbiguousLanguage(p): language = c.getLanguageAtCursor(p, language) d1, d2, d3 = g.set_delims_from_language(language) d2 = d2 or '' d3 = d3 or '' if d1: openDelim, closeDelim = d1 + ' ', '' else: openDelim, closeDelim = d2 + ' ', ' ' + d3 # Comment out non-blank lines. indent = c.config.getBool('indent-added-comments', default=True) result = [] for line in lines: if line.strip(): i = g.skip_ws(line, 0) if indent: result.append(line[0:i] + openDelim + line[i:].replace('\n', '') + closeDelim + '\n') else: result.append(openDelim + line.replace('\n', '') + closeDelim + '\n') else: result.append(line) result = ''.join(result) c.updateBodyPane(head, result, tail, undoType='Add Comments', oldSel=None, oldYview=oldYview)
def __init__(self, importCommands, gen_refs=False, # True: generate section references, language=None, # For @language directive. name=None, # The kind of importer, usually the same as language state_class=None, # For i.scan_line strict=False, **kwargs ): """ Importer.__init__: New in Leo 6.1.1: ic and c may be None for unit tests. """ # Copies of args... self.importCommands = ic = importCommands self.c = c = ic and ic.c self.encoding = ic and ic.encoding or 'utf-8' self.gen_refs = gen_refs self.language = language or name # For the @language directive. self.name = name or language language = self.language name = self.name assert language and name assert self.language and self.name self.state_class = state_class self.strict = strict # True: leading whitespace is significant. # Set from ivars... self.has_decls = name not in ('xml', 'org-mode', 'vimoutliner') self.is_rst = name in ('rst',) self.tree_type = ic.treeType if c else None # '@root', '@file', etc. # Constants... if ic: data = g.set_delims_from_language(self.name) self.single_comment, self.block1, self.block2 = data else: self.single_comment, self.block1, self.block2 = '//', '/*', '*/' # Javascript. if ic: self.escape = c.atFileCommands.underindentEscapeString self.escape_string = r'%s([0-9]+)\.' % re.escape(self.escape) # m.group(1) is the unindent value. self.escape_pattern = re.compile(self.escape_string) self.ScanState = ScanState # Must be set by subclasses that use general_scan_line. self.tab_width = 0 # Must be set in run, using self.root. self.ws_pattern = re.compile(r'^\s*$|^\s*%s' % (self.single_comment or '')) # Settings... self.reloadSettings() # State vars. self.errors = 0 if ic: ic.errors = 0 # Required. self.parse_body = False # Keys are headlines. Values are disambiguating number. self.refs_dict: Dict[str, int] = {} self.root = None self.skip = 0 # A skip count for x.gen_lines & its helpers. self.vnode_info: Dict[str, Any] = {} self.ws_error = False
def flattenOutlineToNode(self, event=None): ''' Append the body text of all descendants of the selected node to the body text of the selected node. ''' c, root, u = self, self.p, self.undoer if not root.hasChildren(): return language = g.getLanguageAtPosition(c, root) if language: single,start,end = g.set_delims_from_language(language) else: single,start,end = '#', None, None bunch = u.beforeChangeNodeContents(root) aList = [] for p in root.subtree(): if single: aList.append('\n\n===== %s %s\n\n' % (single, p.h)) else: aList.append('\n\n===== %s %s %s\n\n' % (start, p.h, end)) if p.b.strip(): lines = g.splitLines(p.b) aList.extend(lines) root.b = root.b.rstrip() + '\n' + ''.join(aList).rstrip() + '\n' u.afterChangeNodeContents(root, 'flatten-outline-to-node', bunch)
def flattenOutlineToNode(self, event=None): ''' Append the body text of all descendants of the selected node to the body text of the selected node. ''' c, root, u = self, self.p, self.undoer if not root.hasChildren(): return language = g.getLanguageAtPosition(c, root) if language: single,start,end = g.set_delims_from_language(language) else: single,start,end = '#', None, None bunch = u.beforeChangeNodeContents(root) aList = [] for p in root.subtree(): if single: aList.append('\n\n===== %s %s\n\n' % (single, p.h)) else: aList.append('\n\n===== %s %s %s\n\n' % (start, p.h, end)) if p.b.strip(): lines = g.splitLines(p.b) aList.extend(lines) root.b = root.b.rstrip() + '\n' + ''.join(aList).rstrip() + '\n' u.afterChangeNodeContents(root, 'flatten-outline-to-node', bunch)
def deleteComments(self, event=None): #@+<< deleteComments docstring >> #@+node:ekr.20171123135625.37: *3* << deleteComments docstring >> #@@pagewidth 50 """ Removes one level of comment delimiters from all selected lines. The applicable @language directive determines the comment delimiters to be removed. Removes single-line comments if possible; removes block comments for languages like html that lack single-line comments. *See also*: add-comments. """ #@-<< deleteComments docstring >> c = self p = c.p head, lines, tail, oldSel, oldYview = self.getBodyLines() result = [] if not lines: g.warning('no text selected') return # The default language in effect at p. language = c.frame.body.colorizer.scanLanguageDirectives(p) if c.hasAmbiguousLanguage(p): language = c.getLanguageAtCursor(p, language) d1, d2, d3 = g.set_delims_from_language(language) if d1: # Remove the single-line comment delim in front of each line d1b = d1 + ' ' n1, n1b = len(d1), len(d1b) for s in lines: i = g.skip_ws(s, 0) if g.match(s, i, d1b): result.append(s[:i] + s[i + n1b :]) elif g.match(s, i, d1): result.append(s[:i] + s[i + n1 :]) else: result.append(s) else: # Remove the block comment delimiters from each line. n2, n3 = len(d2), len(d3) for s in lines: i = g.skip_ws(s, 0) j = s.find(d3, i + n2) if g.match(s, i, d2) and j > -1: first = i + n2 if g.match(s, first, ' '): first += 1 last = j if g.match(s, last - 1, ' '): last -= 1 result.append(s[:i] + s[first:last] + s[j + n3 :]) else: result.append(s) result = ''.join(result) c.updateBodyPane( head, result, tail, undoType='Delete Comments', oldSel=None, oldYview=oldYview)
def deleteComments(self, event=None): #@+<< deleteComments docstring >> #@+node:ekr.20171123135625.37: *3* << deleteComments docstring >> #@@pagewidth 50 ''' Removes one level of comment delimiters from all selected lines. The applicable @language directive determines the comment delimiters to be removed. Removes single-line comments if possible; removes block comments for languages like html that lack single-line comments. *See also*: add-comments. ''' #@-<< deleteComments docstring >> c = self p = c.p head, lines, tail, oldSel, oldYview = self.getBodyLines() result = [] if not lines: g.warning('no text selected') return # The default language in effect at p. language = c.frame.body.colorizer.scanLanguageDirectives(p) if c.hasAmbiguousLanguage(p): language = c.getLanguageAtCursor(p, language) d1, d2, d3 = g.set_delims_from_language(language) if d1: # Remove the single-line comment delim in front of each line d1b = d1 + ' ' n1, n1b = len(d1), len(d1b) for s in lines: i = g.skip_ws(s, 0) if g.match(s, i, d1b): result.append(s[: i] + s[i + n1b:]) elif g.match(s, i, d1): result.append(s[: i] + s[i + n1:]) else: result.append(s) else: # Remove the block comment delimiters from each line. n2, n3 = len(d2), len(d3) for s in lines: i = g.skip_ws(s, 0) j = s.find(d3, i + n2) if g.match(s, i, d2) and j > -1: first = i + n2 if g.match(s, first, ' '): first += 1 last = j if g.match(s, last - 1, ' '): last -= 1 result.append(s[: i] + s[first: last] + s[j + n3:]) else: result.append(s) result = ''.join(result) c.updateBodyPane(head, result, tail, undoType='Delete Comments', oldSel=None, oldYview=oldYview)
def __init__(self, importCommands, gen_refs=False, # True: generate section references, language=None, # For @language directive. name=None, # The kind of importer, usually the same as language state_class=None, # For i.scan_line strict=False, **kwargs ): '''Importer.__init__.''' # Copies of args... self.importCommands = ic = importCommands self.c = c = ic.c self.encoding = ic.encoding self.gen_refs = gen_refs self.language = language or name # For the @language directive. self.name = name or language language = self.language name = self.name assert language and name assert self.language and self.name self.state_class = state_class self.strict = strict # True: leading whitespace is significant. # # Set from ivars... self.has_decls = name not in ('xml', 'org-mode', 'vimoutliner') self.is_rst = name in ('rst',) self.tree_type = ic.treeType # '@root', '@file', etc. # # Constants... data = g.set_delims_from_language(self.name) self.single_comment, self.block1, self.block2 = data self.escape = c.atFileCommands.underindentEscapeString self.escape_string = r'%s([0-9]+)\.' % re.escape(self.escape) # m.group(1) is the unindent value. self.escape_pattern = re.compile(self.escape_string) self.ScanState = ScanState # Must be set by subclasses that use general_scan_line. self.tab_width = 0 # Must be set in run, using self.root. self.ws_pattern = re.compile(r'^\s*$|^\s*%s' % (self.single_comment or '')) # # Settings... self.reloadSettings() # # State vars. self.errors = 0 ic.errors = 0 # Required. self.parse_body = False self.refs_dict = {} # Keys are headlines. Values are disambiguating number. self.skip = 0 # A skip count for x.gen_lines & its helpers. self.ws_error = False self.root = None
def addComments(self, event=None): #@+<< addComments docstring >> #@+node:ekr.20171123135625.35: *3* << addComments docstring >> #@@pagewidth 50 ''' Converts all selected lines to comment lines using the comment delimiters given by the applicable @language directive. Inserts single-line comments if possible; inserts block comments for languages like html that lack single-line comments. @bool indent_added_comments If True (the default), inserts opening comment delimiters just before the first non-whitespace character of each line. Otherwise, inserts opening comment delimiters at the start of each line. *See also*: delete-comments. ''' #@-<< addComments docstring >> c = self; p = c.p head, lines, tail, oldSel, oldYview = self.getBodyLines() if not lines: g.warning('no text selected') return # The default language in effect at p. language = c.frame.body.colorizer.scanLanguageDirectives(p) if c.hasAmbiguousLanguage(p): language = c.getLanguageAtCursor(p, language) d1, d2, d3 = g.set_delims_from_language(language) d2 = d2 or ''; d3 = d3 or '' if d1: openDelim, closeDelim = d1 + ' ', '' else: openDelim, closeDelim = d2 + ' ', ' ' + d3 # Comment out non-blank lines. indent = c.config.getBool('indent-added-comments', default=True) result = [] for line in lines: if line.strip(): i = g.skip_ws(line, 0) if indent: result.append(line[0: i] + openDelim + line[i:].replace('\n', '') + closeDelim + '\n') else: result.append(openDelim + line.replace('\n', '') + closeDelim + '\n') else: result.append(line) result = ''.join(result) c.updateBodyPane(head, result, tail, undoType='Add Comments', oldSel=None, oldYview=oldYview)
def deleteComments(self, event=None): #@+<< deleteComments docstring >> #@+node:ekr.20171123135625.37: *3* << deleteComments docstring >> #@@pagewidth 50 """ Removes one level of comment delimiters from all selected lines. The applicable @language directive determines the comment delimiters to be removed. Removes single-line comments if possible; removes block comments for languages like html that lack single-line comments. *See also*: add-comments. """ #@-<< deleteComments docstring >> c, p, u, w = self, self.p, self.undoer, self.frame.body.wrapper # # "Before" snapshot. bunch = u.beforeChangeBody(p) # # Initial data. head, lines, tail, oldSel, oldYview = self.getBodyLines() if not lines: g.warning('no text selected') return # The default language in effect at p. language = c.frame.body.colorizer.scanLanguageDirectives(p) if c.hasAmbiguousLanguage(p): language = c.getLanguageAtCursor(p, language) d1, d2, d3 = g.set_delims_from_language(language) # # Calculate the result. changed, result = False, [] if d1: # Remove the single-line comment delim in front of each line d1b = d1 + ' ' n1, n1b = len(d1), len(d1b) for s in lines: i = g.skip_ws(s, 0) if g.match(s, i, d1b): result.append(s[:i] + s[i + n1b:]) changed = True elif g.match(s, i, d1): result.append(s[:i] + s[i + n1:]) changed = True else: result.append(s) else: # Remove the block comment delimiters from each line. n2, n3 = len(d2), len(d3) for s in lines: i = g.skip_ws(s, 0) j = s.find(d3, i + n2) if g.match(s, i, d2) and j > -1: first = i + n2 if g.match(s, first, ' '): first += 1 last = j if g.match(s, last - 1, ' '): last -= 1 result.append(s[:i] + s[first:last] + s[j + n3:]) changed = True else: result.append(s) if not changed: return # # Set p.b and w's text first. middle = ''.join(result) p.b = head + middle + tail # Sets dirty and changed bits. w.setAllText(head + middle + tail) # # Set the selection range and scroll position. i = len(head) j = ins = max(i, len(head) + len(middle) - 1) w.setSelectionRange(i, j, insert=ins) w.setYScrollPosition(oldYview) # # "after" snapshot. u.afterChangeBody(p, 'Indent Region', bunch)
def addComments(self, event=None): #@+<< addComments docstring >> #@+node:ekr.20171123135625.35: *3* << addComments docstring >> #@@pagewidth 50 """ Converts all selected lines to comment lines using the comment delimiters given by the applicable @language directive. Inserts single-line comments if possible; inserts block comments for languages like html that lack single-line comments. @bool indent_added_comments If True (the default), inserts opening comment delimiters just before the first non-whitespace character of each line. Otherwise, inserts opening comment delimiters at the start of each line. *See also*: delete-comments. """ #@-<< addComments docstring >> c, p, u, w = self, self.p, self.undoer, self.frame.body.wrapper # # "Before" snapshot. bunch = u.beforeChangeBody(p) # # Make sure there is a selection. head, lines, tail, oldSel, oldYview = self.getBodyLines() if not lines: g.warning('no text selected') return # # The default language in effect at p. language = c.frame.body.colorizer.scanLanguageDirectives(p) if c.hasAmbiguousLanguage(p): language = c.getLanguageAtCursor(p, language) d1, d2, d3 = g.set_delims_from_language(language) d2 = d2 or '' d3 = d3 or '' if d1: openDelim, closeDelim = d1 + ' ', '' else: openDelim, closeDelim = d2 + ' ', ' ' + d3 # # Calculate the result. indent = c.config.getBool('indent-added-comments', default=True) result = [] for line in lines: if line.strip(): i = g.skip_ws(line, 0) if indent: s = line[i:].replace('\n', '') result.append(line[0:i] + openDelim + s + closeDelim + '\n') else: s = line.replace('\n', '') result.append(openDelim + s + closeDelim + '\n') else: result.append(line) # # Set p.b and w's text first. middle = ''.join(result) p.b = head + middle + tail # Sets dirty and changed bits. w.setAllText(head + middle + tail) # # Calculate the proper selection range (i, j, ins). i = len(head) j = max(i, len(head) + len(middle) - 1) # # Set the selection range and scroll position. w.setSelectionRange(i, j, insert=j) w.setYScrollPosition(oldYview) # # "after" snapshot. u.afterChangeBody(p, 'Add Comments', bunch)