def _determine_comment_style(self): # I'm not exactly sure why this function needs a point. It seems to # return the same value regardless of location for the stuff I've # tried. (self._lc, self._bc) = comment.build_comment_data(self.view, 0) if (u'//', False) in self._lc: # Doxygen-style comment blocks self._lc = [('/// ', False), ('///', False), ('//! ', False), ('//!', False)] + self._lc
def get_comment_settings(self): """Determines the appropriate block comment characters for the currently selected syntax.""" lineComments, blockComments = comment.build_comment_data(self.view, 0) lang = self.get_language_descriptor() overrideLanguages = self.settings.get(constants.SETTING_LANGUAGES_USE_LINE_COMMENTS) if len(lineComments) == 0 and len(blockComments) == 0: self.firstLine = '# ' self.middleLine = '# ' self.lastLine = '# ' elif lang in overrideLanguages or (len(blockComments) == 0 and len(lineComments) > 0): self.firstLine = lineComments[0][0] self.middleLine = lineComments[0][0] self.lastLine = lineComments[0][0] else: self.firstLine = blockComments[0][0] self.middleLine = '' self.lastLine = blockComments[0][1]
def makeSeparator( self, size ): view = sublime.active_window().active_view() global comment lineComment = "//" line_comments = comment.build_comment_data( view, view.sel()[0].a )[0] if len(line_comments) > 0: lineComment = line_comments[0][0].strip() ret = [] separatorTypes = self.getSeparatorTypes() for i in range( 0, len( separatorTypes ) ): addComment = lineComment + separatorTypes[i] * size ret.append( addComment ) return ret
def expand_to_paragraph(view, tp): sr = view.full_line(tp) if is_paragraph_separating_line(view, sr): return sublime.Region(tp, tp) required_prefix = None # If the current line starts with a comment, only select lines that are also # commented (line_comments, block_comments) = comment.build_comment_data(view, tp) dataStart = comment.advance_to_first_non_white_space_on_line( view, sr.begin()) for c in line_comments: (start, disable_indent) = c comment_region = sublime.Region(dataStart, dataStart + len(start)) if view.substr(comment_region) == start: required_prefix = view.substr( sublime.Region(sr.begin(), comment_region.end())) break first = sr.begin() prev = sr while True: prev = previous_line(view, prev) if (prev == None or is_paragraph_separating_line(view, prev) or not has_prefix(view, prev, required_prefix)): break else: first = prev.begin() last = sr.end() next = sr while True: next = next_line(view, next) if (next == None or is_paragraph_separating_line(view, next) or not has_prefix(view, next, required_prefix)): break else: last = next.end() return sublime.Region(first, last)
def expand_to_paragraph(view, tp): sr = view.full_line(tp) if is_paragraph_separating_line(view, sr): return sublime.Region(tp, tp) required_prefix = None # If the current line starts with a comment, only select lines that are also # commented (line_comments, block_comments) = comment.build_comment_data(view, tp) dataStart = comment.advance_to_first_non_white_space_on_line(view, sr.begin()) for c in line_comments: (start, disable_indent) = c comment_region = sublime.Region(dataStart, dataStart + len(start)) if view.substr(comment_region) == start: required_prefix = view.substr(sublime.Region(sr.begin(), comment_region.end())) break first = sr.begin() prev = sr while True: prev = previous_line(view, prev) if (prev == None or is_paragraph_separating_line(view, prev) or not has_prefix(view, prev, required_prefix)): break else: first = prev.begin() last = sr.end() next = sr while True: next = next_line(view, next) if (next == None or is_paragraph_separating_line(view, next) or not has_prefix(view, next, required_prefix)): break else: last = next.end() return sublime.Region(first, last)
def decorate( self, edit, currentSelection ): lineComments, blockComments = comment.build_comment_data(self.view, 0) self.firstLine = blockComments[0][0] self.lastLine = blockComments[0][1] # Convert the input range to a string, this represents the original selection. original = self.view.substr( currentSelection ); # Construct a local path to the fonts directory. fontsDir = os.path.join(sublime.packages_path(), 'ASCII Decorator', 'pyfiglet', 'fonts') # Convert the input string to ASCII Art. settings = sublime.load_settings('ASCII Decorator.sublime-settings') f = Figlet( dir=fontsDir, font=settings.get('ascii_decorator_font') ) output = f.renderText( original ); # Normalize line endings based on settings. output = self.normalize_line_endings( output ) # Normalize whitespace based on settings. output = self.fix_whitespace( original, output, currentSelection ) self.view.replace( edit, currentSelection, self.firstLine + "\n" + output + "\n" + self.lastLine ) return sublime.Region( currentSelection.begin(), currentSelection.begin() + len(output) )
def _determine_comment_style(self): # I'm not exactly sure why this function needs a point. It seems to # return the same value regardless of location for the stuff I've # tried. (self._lc, self._bc) = comment.build_comment_data(self.view, 0)
def onDoneSelector( self, default, index ): if index < 0: return view = sublime.active_window().active_view() if sublime.active_window() else None if view is None: return # select index. items = self.makeQuickPanelItem(default) separatorTypeIndex = -1 mode = "None" match = re.search( "^([0-9rz]+)", items[index] ) if match: if match.group(1) == "z": separatorTypeIndex = len(items) - 2 mode = "undo" elif match.group(1) == "r": separatorTypeIndex = len(items) - 1 mode = "redo" else: separatorTypeIndex = int( match.group(1) ) mode = "comment" # process if mode == "undo": # undo. view.window().run_command( "undo" ) elif mode == "redo": # redo. view.window().run_command( "redo" ) elif separatorTypeIndex >= 0: # insert separator. r, c = view.rowcol( view.sel()[0].begin() ) linehead = view.text_point( r, 0 ) indentString = view.substr( sublime.Region( linehead, view.sel()[0].begin() ) ) tabSize = view.settings().get( "tab_size" ) tab = tabSize indentLength = 0 for char in indentString: if char == "\t": indentLength += tab tab = tabSize else: indentLength += 1 tab -= 1 if tab <= 0: tab = tabSize global comment commentLength = 2 line_comments = comment.build_comment_data( view, view.sel()[0].a )[0] if len(line_comments) > 0: commentLength = len( line_comments[0][0].strip() ) separatorLength = 80 if view.settings().has( "separator_length" ): separatorLength = view.settings().get( "separator_length" ) expandLength = separatorLength - commentLength - indentLength separatorMultiByteCount = count_zenkaku( self.getSeparatorTypes()[separatorTypeIndex].decode( "utf-8" ) ) separatorSingleByteCount = len( self.getSeparatorTypes()[separatorTypeIndex].decode( "UTF-8" ) ) - separatorMultiByteCount separatorDefineLength = separatorMultiByteCount * 2 + separatorSingleByteCount separators = self.makeSeparator( expandLength / separatorDefineLength ) addString = separators[separatorTypeIndex] + "\n" + indentString edit = view.begin_edit( "separator", None ) view.insert( edit, view.sel()[0].begin(), addString ) view.end_edit( edit ) # continue process. self.openSelector( separatorTypeIndex ) return