src/eric7/QScintilla/Editor.py

branch
eric7
changeset 9331
1f8780a59a0d
parent 9221
bf71ee032bb4
child 9333
b0541ec21e51
equal deleted inserted replaced
9330:a5b42af40217 9331:1f8780a59a0d
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.

eric ide

mercurial