src/eric7/QScintilla/Editor.py

branch
eric7-maintenance
changeset 9371
1da8bc75946f
parent 9304
afbd61b99bf4
parent 9333
b0541ec21e51
child 9442
906485dcd210
equal deleted inserted replaced
9306:90dc02c8a384 9371:1da8bc75946f
3922 3922
3923 def toggleCommentBlock(self): 3923 def toggleCommentBlock(self):
3924 """ 3924 """
3925 Public slot to toggle the comment of a block. 3925 Public slot to toggle the comment of a block.
3926 3926
3927 If the line of the cursor or the selection is not commented, it will 3927 If the editor contains selected text and the start line is not commented, it
3928 be commented. If it is commented, the comment block will be removed. 3928 will be commented. Otherwise the selection will be un-commented. In case there
3929 The later works independent of the current selection. 3929 is no selected text and the current line is not commented, it will be commented.
3930 If is commented, the comment block will be removed.
3930 """ 3931 """
3931 if self.lexer_ is None or not self.lexer_.canBlockComment(): 3932 if self.lexer_ is None or not self.lexer_.canBlockComment():
3932 return 3933 return
3933 3934
3934 commentStr = self.lexer_.commentStr() 3935 commentStr = self.lexer_.commentStr()
3935 line, index = self.getCursorPosition() 3936 line, index = self.getCursorPosition()
3936 3937
3937 # check if line starts with our comment string (i.e. was commented 3938 if self.hasSelectedText():
3938 # by our comment...() slots 3939 # Check if the selection starts with our comment string (i.e. was commented
3939 if self.hasSelectedText() and self.__isCommentedLine( 3940 # by our comment...() slots.
3940 self.text(self.getSelection()[0]), commentStr 3941 if self.__isCommentedLine(self.text(self.getSelection()[0]), commentStr):
3941 ): 3942 self.uncommentLineOrSelection()
3942 self.uncommentLineOrSelection() 3943 else:
3944 self.commentLineOrSelection()
3943 elif not self.__isCommentedLine(self.text(line), commentStr): 3945 elif not self.__isCommentedLine(self.text(line), commentStr):
3944 # it doesn't, so comment the line or selection 3946 # No selected text and the current line does not start with our comment
3947 # string, so comment the line.
3945 self.commentLineOrSelection() 3948 self.commentLineOrSelection()
3946 else: 3949 else:
3947 # determine the start of the comment block 3950 # Uncomment the comment block containing the current line.
3951 # 1. determine the start of the comment block
3948 begline = line 3952 begline = line
3949 while begline > 0 and self.__isCommentedLine( 3953 while begline > 0 and self.__isCommentedLine(
3950 self.text(begline - 1), commentStr 3954 self.text(begline - 1), commentStr
3951 ): 3955 ):
3952 begline -= 1 3956 begline -= 1
3953 # determine the end of the comment block 3957 # 2. determine the end of the comment block
3954 endline = line 3958 endline = line
3955 lines = self.lines() 3959 lines = self.lines()
3956 while endline < lines and self.__isCommentedLine( 3960 while endline < lines and self.__isCommentedLine(
3957 self.text(endline + 1), commentStr 3961 self.text(endline + 1), commentStr
3958 ): 3962 ):
3959 endline += 1 3963 endline += 1
3960 3964
3965 # 3. uncomment the determined block and reset the cursor position
3961 self.setSelection(begline, 0, endline, self.lineLength(endline)) 3966 self.setSelection(begline, 0, endline, self.lineLength(endline))
3962 self.uncommentLineOrSelection() 3967 self.uncommentLineOrSelection()
3963
3964 # reset the cursor
3965 self.setCursorPosition(line, index - len(commentStr)) 3968 self.setCursorPosition(line, index - len(commentStr))
3966 3969
3967 def commentLine(self): 3970 def commentLine(self):
3968 """ 3971 """
3969 Public slot to comment the current line. 3972 Public slot to comment the current line.
7775 matchingPairs = ["()", "[]", "{}", "<>", "''", '""'] 7778 matchingPairs = ["()", "[]", "{}", "<>", "''", '""']
7776 # __IGNORE_WARNING_M613__ 7779 # __IGNORE_WARNING_M613__
7777 if text in matchingPairs: 7780 if text in matchingPairs:
7778 self.delete() 7781 self.delete()
7779 7782
7783 elif (
7784 cmd in (QsciScintilla.SCI_LOWERCASE, QsciScintilla.SCI_UPPERCASE)
7785 and self.hasSelectedText()
7786 ):
7787 startLine, startIndex, endLine, _ = self.getSelection()
7788 selectedText = self.selectedText()
7789 replacementText = (
7790 selectedText.upper()
7791 if cmd == QsciScintilla.SCI_UPPERCASE
7792 else selectedText.lower()
7793 )
7794 self.replaceSelectedText(replacementText)
7795 self.setSelection(
7796 startLine, startIndex, endLine, len(replacementText.splitlines()[-1])
7797 )
7798 return
7799
7780 super().editorCommand(cmd) 7800 super().editorCommand(cmd)
7781 7801
7782 def __applyTemplate(self, templateName, language): 7802 def __applyTemplate(self, templateName, language):
7783 """ 7803 """
7784 Private method to apply a template by name. 7804 Private method to apply a template by name.

eric ide

mercurial