1452 indent_chances[start[1]] = text |
1452 indent_chances[start[1]] = text |
1453 |
1453 |
1454 last_token_multiline = (start[0] != end[0]) |
1454 last_token_multiline = (start[0] != end[0]) |
1455 |
1455 |
1456 return valid_indents |
1456 return valid_indents |
|
1457 |
|
1458 |
|
1459 class Pep8LineShortener(object): |
|
1460 """ |
|
1461 Class used to shorten lines to a given maximum of characters. |
|
1462 """ |
|
1463 def __init__(self, curLine, prevLine, nextLine, |
|
1464 maxLength=79, eol="\n", indentWord=" "): |
|
1465 """ |
|
1466 Constructor |
|
1467 |
|
1468 @param curLine text to work on (string) |
|
1469 @param prevLine line before the text to work on (string) |
|
1470 @param nextLine line after the text to work on (string) |
|
1471 @keyparam maxLength maximum allowed line length (integer) |
|
1472 @keyparam eol eond-of-line marker (string) |
|
1473 @keyparam indentWord string used for indentation (string) |
|
1474 """ |
|
1475 self.__text = curLine |
|
1476 self.__prevText = prevLine |
|
1477 self.__nextText = nextLine |
|
1478 self.__maxLength = maxLength |
|
1479 self.__eol = eol |
|
1480 self.__indentWord = indentWord |
|
1481 |
|
1482 def shorten(self): |
|
1483 """ |
|
1484 Public method to shorten the line wrapped by the class instance. |
|
1485 |
|
1486 @return tuple of a flag indicating successful shortening and the shortened line |
|
1487 (boolean, string) |
|
1488 """ |
|
1489 # 1. check for comment |
|
1490 if self.__text.lstrip().startswith('#'): |
|
1491 lastComment = True |
|
1492 if self.__nextText.lstrip().startswith('#'): |
|
1493 lastComment = False |
|
1494 |
|
1495 # Wrap commented lines. |
|
1496 newText = self.__shortenComment(lastComment) |
|
1497 return True, newText |
|
1498 |
|
1499 indent = self.__getIndent(self.__text) |
|
1500 source = self.__text[len(indent):] |
|
1501 assert source.lstrip() == source |
|
1502 sio = io.StringIO(source) |
|
1503 |
|
1504 # Check for multiline string. |
|
1505 try: |
|
1506 tokens = list(tokenize.generate_tokens(sio.readline)) |
|
1507 except (SyntaxError, tokenize.TokenError): |
|
1508 multilineCandidate = self.__breakMultiline() |
|
1509 if multilineCandidate: |
|
1510 return True, multilineCandidate |
|
1511 else: |
|
1512 return False, "" |
|
1513 |
|
1514 # Handle statements by putting the right hand side on a line by itself. |
|
1515 # This should let the next pass shorten it. |
|
1516 if source.startswith('return '): |
|
1517 newText = ( |
|
1518 indent + |
|
1519 'return (' + |
|
1520 self.__eol + |
|
1521 indent + self.__indentWord + re.sub('^return ', '', source) + |
|
1522 indent + ')' + self.__eol |
|
1523 ) |
|
1524 return True, newText |
|
1525 |
|
1526 candidates = self.__shortenLine(tokens, source, indent) |
|
1527 candidates = list(sorted( |
|
1528 set(candidates).union([self.__text]), |
|
1529 key=lambda x: self.__lineShorteningRank(x))) |
|
1530 if candidates: |
|
1531 return True, candidates[0] |
|
1532 |
|
1533 return False, "" |
|
1534 |
|
1535 def __shortenComment(self, isLast): |
|
1536 """ |
|
1537 Private method to shorten a comment line. |
|
1538 |
|
1539 @param isLast flag indicating, that the line is the last comment line |
|
1540 (boolean) |
|
1541 @return shortened comment line (string) |
|
1542 """ |
|
1543 pass |
|
1544 |
|
1545 def __getIndent(self, line): |
|
1546 """ |
|
1547 Private method to get the indentation string. |
|
1548 |
|
1549 @param line line to determine the indentation string from (string) |
|
1550 @return indentation string (string) |
|
1551 """ |
|
1552 # copied from Pep8Fixer |
|
1553 return line.replace(line.lstrip(), "") |