def regularizeWhitespace(self, s): '''Regularize leading whitespace in s: Convert tabs to blanks or vice versa depending on the @tabwidth in effect. This is only called for strict languages.''' changed = False lines = g.splitLines(s) result = [] tab_width = self.tab_width if tab_width < 0: # Convert tabs to blanks. for line in lines: i, w = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace( w, -abs(tab_width)) + line[i:] # Use negative width. if s != line: changed = True result.append(s) elif tab_width > 0: # Convert blanks to tabs. for line in lines: s = g.optimizeLeadingWhitespace( line, abs(tab_width)) # Use positive width. if s != line: changed = True result.append(s) if changed: action = 'tabs converted to blanks' if self.tab_width < 0 else 'blanks converted to tabs' message = 'inconsistent leading whitespace. %s' % action self.report(message) return ''.join(result)
def regularize_whitespace(self, lines): """ Regularize leading whitespace in s: Convert tabs to blanks or vice versa depending on the @tabwidth in effect. """ kind = 'tabs' if self.tab_width > 0 else 'blanks' kind2 = 'blanks' if self.tab_width > 0 else 'tabs' fn = g.shortFileName(self.root.h) count, result, tab_width = 0, [], self.tab_width self.ws_error = False # 2016/11/23 if tab_width < 0: # Convert tabs to blanks. for n, line in enumerate(lines): i, w = g.skip_leading_ws_with_indent(line, 0, tab_width) # Use negative width. s = g.computeLeadingWhitespace(w, -abs(tab_width)) + line[i:] if s != line: count += 1 result.append(s) elif tab_width > 0: # Convert blanks to tabs. for n, line in enumerate(lines): # Use positive width. s = g.optimizeLeadingWhitespace(line, abs(tab_width)) if s != line: count += 1 result.append(s) if count: self.ws_error = True # A flag to check. if not g.unitTesting: # g.es_print('Warning: Intermixed tabs and blanks in', fn) # g.es_print('Perfect import test will ignoring leading whitespace.') g.es('changed leading %s to %s in %s line%s in %s' % ( kind2, kind, count, g.plural(count), fn)) if g.unitTesting: # Sets flag for unit tests. self.report('changed %s lines' % count) return result
def convertBlanks(self, event=None): '''Convert all blanks to tabs in the selected node.''' c = self; changed = False; dirtyVnodeList = [] head, lines, tail, oldSel, oldYview = c.getBodyLines(expandSelection=True) # Use the relative @tabwidth, not the global one. theDict = c.scanAllDirectives() tabWidth = theDict.get("tabwidth") if tabWidth: result = [] for line in lines: s = g.optimizeLeadingWhitespace(line, abs(tabWidth)) # Use positive width. if s != line: changed = True result.append(s) if changed: undoType = 'Convert Blanks' result = ''.join(result) oldSel = None dirtyVnodeList = c.updateBodyPane(head, result, tail, undoType, oldSel, oldYview) # Handles undo return changed, dirtyVnodeList
def convertBlanks(self, event=None): """ Convert *all* blanks to tabs in the selected node. Return True if the the p.b was changed. """ c, p, u, w = self, self.p, self.undoer, self.frame.body.wrapper # # "Before" snapshot. bunch = u.beforeChangeBody(p) oldYview = w.getYScrollPosition() w.selectAllText() head, lines, tail, oldSel, oldYview = c.getBodyLines() # # Use the relative @tabwidth, not the global one. d = c.scanAllDirectives(p) tabWidth = d.get("tabwidth") if not tabWidth: return False # # Calculate the result. changed, result = False, [] for line in lines: s = g.optimizeLeadingWhitespace(line, abs(tabWidth)) # Use positive width. if s != line: changed = True result.append(s) if not changed: return False # # 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) # # Select all text and set scroll position. w.selectAllText() w.setYScrollPosition(oldYview) # # "after" snapshot. u.afterChangeBody(p, 'Indent Region', bunch) return True
def regularizeWhitespace(self, s): '''Regularize leading whitespace in s: Convert tabs to blanks or vice versa depending on the @tabwidth in effect. This is only called for strict languages.''' changed = False; lines = g.splitLines(s); result = []; tab_width = self.tab_width if tab_width < 0: # Convert tabs to blanks. for line in lines: i, w = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(w, -abs(tab_width)) + line[i:] # Use negative width. if s != line: changed = True result.append(s) elif tab_width > 0: # Convert blanks to tabs. for line in lines: s = g.optimizeLeadingWhitespace(line, abs(tab_width)) # Use positive width. if s != line: changed = True result.append(s) if changed: action = 'tabs converted to blanks' if self.tab_width < 0 else 'blanks converted to tabs' message = 'inconsistent leading whitespace. %s' % action self.report(message) return ''.join(result)
def regularize_whitespace(self, s): ''' Regularize leading whitespace in s: Convert tabs to blanks or vice versa depending on the @tabwidth in effect. ''' kind = 'tabs' if self.tab_width > 0 else 'blanks' kind2 = 'blanks' if self.tab_width > 0 else 'tabs' fn = g.shortFileName(self.root.h) lines = g.splitLines(s) count, result, tab_width = 0, [], self.tab_width self.ws_error = False # 2016/11/23 if tab_width < 0: # Convert tabs to blanks. for n, line in enumerate(lines): i, w = g.skip_leading_ws_with_indent(line, 0, tab_width) s = g.computeLeadingWhitespace(w, -abs(tab_width)) + line[i:] # Use negative width. if s != line: count += 1 result.append(s) elif tab_width > 0: # Convert blanks to tabs. for n, line in enumerate(lines): s = g.optimizeLeadingWhitespace(line, abs(tab_width)) # Use positive width. if s != line: count += 1 result.append(s) if count: self.ws_error = True # A flag to check. if not g.unitTesting: # g.es_print('Warning: Intermixed tabs and blanks in', fn) # g.es_print('Perfect import test will ignoring leading whitespace.') g.es('changed leading %s to %s in %s line%s in %s' % ( kind2, kind, count, g.plural(count), fn)) if g.unitTesting: # Sets flag for unit tests. self.report('changed %s lines' % count) return ''.join(result)