QScintilla/Editor.py

changeset 6105
cbd34d558bd9
parent 6101
b854a825d483
child 6111
d38b38117d83
equal deleted inserted replaced
6102:a5b9f6a38faf 6105:cbd34d558bd9
192 # key: marker handle 192 # key: marker handle
193 # value: list of (warning message, warning type) 193 # value: list of (warning message, warning type)
194 self.notcoveredMarkers = [] # just a list of marker handles 194 self.notcoveredMarkers = [] # just a list of marker handles
195 self.showingNotcoveredMarkers = False 195 self.showingNotcoveredMarkers = False
196 196
197 self.lexer_ = None
198
197 self.__loadEditorConfig() 199 self.__loadEditorConfig()
198 200
199 self.condHistory = [] 201 self.condHistory = []
200 self.lexer_ = None
201 self.__lexerReset = False 202 self.__lexerReset = False
202 self.completer = None 203 self.completer = None
203 self.encoding = self.__getEditorConfig("DefaultEncoding") 204 self.encoding = self.__getEditorConfig("DefaultEncoding")
204 self.apiLanguage = '' 205 self.apiLanguage = ''
205 self.lastModified = 0 206 self.lastModified = 0
2958 @keyparam encoding encoding to be used to read the file (string) 2959 @keyparam encoding encoding to be used to read the file (string)
2959 (Note: this parameter overrides encoding detection) 2960 (Note: this parameter overrides encoding detection)
2960 """ 2961 """
2961 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) 2962 QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
2962 2963
2964 self.__loadEditorConfig(fileName=fn)
2965
2963 try: 2966 try:
2964 if createIt and not os.path.exists(fn): 2967 if createIt and not os.path.exists(fn):
2965 f = open(fn, "w") 2968 f = open(fn, "w")
2966 f.close() 2969 f.close()
2967 if encoding == "": 2970 if encoding == "":
2980 self.tr('<p>The file <b>{0}</b> could not be opened.</p>' 2983 self.tr('<p>The file <b>{0}</b> could not be opened.</p>'
2981 '<p>Reason: {1}</p>') 2984 '<p>Reason: {1}</p>')
2982 .format(fn, str(why))) 2985 .format(fn, str(why)))
2983 QApplication.restoreOverrideCursor() 2986 QApplication.restoreOverrideCursor()
2984 raise 2987 raise
2985 fileEol = self.detectEolString(txt)
2986 2988
2987 modified = False 2989 modified = False
2988 # TODO: editorconfig: indent_style 2990
2989 if (not Preferences.getEditor("TabForIndentation")) and \ 2991 if (not self.__getEditorConfig("TabForIndentation")) and \
2990 Preferences.getEditor("ConvertTabsOnLoad") and \ 2992 Preferences.getEditor("ConvertTabsOnLoad") and \
2991 not (self.lexer_ and 2993 not (self.lexer_ and
2992 self.lexer_.alwaysKeepTabs()): 2994 self.lexer_.alwaysKeepTabs()):
2993 txtExpanded = txt.expandtabs(Preferences.getEditor("TabWidth")) 2995 txtExpanded = txt.expandtabs(self.__getEditorConfig("TabWidth"))
2994 if txtExpanded != txt: 2996 if txtExpanded != txt:
2995 modified = True 2997 modified = True
2996 txt = txtExpanded 2998 txt = txtExpanded
2997 del txtExpanded
2998 2999
2999 self.setText(txt) 3000 self.setText(txt)
3000 3001
3001 # get eric specific flags 3002 # get eric specific flags
3002 self.__processFlags() 3003 self.__processFlags()
3003 3004
3004 # perform automatic eol conversion 3005 # perform automatic EOL conversion
3005 # TODO: editorconfig: end_of_line
3006 if self.__getEditorConfig("EOLMode", nodefault=True) or \ 3006 if self.__getEditorConfig("EOLMode", nodefault=True) or \
3007 Preferences.getEditor("AutomaticEOLConversion"): 3007 Preferences.getEditor("AutomaticEOLConversion"):
3008 self.convertEols(self.eolMode()) 3008 self.convertEols(self.eolMode())
3009 else: 3009 else:
3010 fileEol = self.detectEolString(txt)
3010 self.setEolModeByEolString(fileEol) 3011 self.setEolModeByEolString(fileEol)
3011 3012
3012 self.extractTasks() 3013 self.extractTasks()
3013 3014
3014 QApplication.restoreOverrideCursor() 3015 QApplication.restoreOverrideCursor()
3035 3036
3036 @param fn filename to write to (string) 3037 @param fn filename to write to (string)
3037 @param backup flag indicating to save a backup (boolean) 3038 @param backup flag indicating to save a backup (boolean)
3038 @return flag indicating success (boolean) 3039 @return flag indicating success (boolean)
3039 """ 3040 """
3040 # TODO: editorconfig: apply end_of_line, trim_trailing_whitespace,
3041 # insert_final_newline
3042 config = self.__loadEditorConfigObject(fn) 3041 config = self.__loadEditorConfigObject(fn)
3043 3042
3044 # TODO: editorconfig: trim_trailing_whitespace 3043 eol = self.__getEditorConfig("EOLMode", nodefault=True, config=config)
3045 if Preferences.getEditor("StripTrailingWhitespace"): 3044 if eol is not None:
3045 self.convertEols(eol)
3046
3047 if self.__getEditorConfig("StripTrailingWhitespace", config=config):
3046 self.__removeTrailingWhitespace() 3048 self.__removeTrailingWhitespace()
3047 3049
3048 txt = self.text() 3050 txt = self.text()
3049 # work around glitch in scintilla: always make sure, 3051
3050 # that the last line is terminated properly 3052 if self.__getEditorConfig("InsertFinalNewline", config=config):
3051 # TODO: editorconfig: insert_final_newline 3053 eol = self.getLineSeparator()
3052 eol = self.getLineSeparator() 3054 if eol:
3053 if eol: 3055 if len(txt) >= len(eol):
3054 if len(txt) >= len(eol): 3056 if txt[-len(eol):] != eol:
3055 if txt[-len(eol):] != eol: 3057 txt += eol
3058 else:
3056 txt += eol 3059 txt += eol
3057 else:
3058 txt += eol
3059 3060
3060 # create a backup file, if the option is set 3061 # create a backup file, if the option is set
3061 createBackup = backup and Preferences.getEditor("CreateBackupFile") 3062 createBackup = backup and Preferences.getEditor("CreateBackupFile")
3062 if createBackup: 3063 if createBackup:
3063 if os.path.islink(fn): 3064 if os.path.islink(fn):
3203 newName = fn 3204 newName = fn
3204 3205
3205 # save to project, if a project is loaded 3206 # save to project, if a project is loaded
3206 if self.project.isOpen() and \ 3207 if self.project.isOpen() and \
3207 self.project.startswithProjectPath(fn): 3208 self.project.startswithProjectPath(fn):
3208 # TODO: check against editorconfig 3209 editorConfigEol = self.__getEditorConfig(
3209 self.setEolModeByEolString(self.project.getEolString()) 3210 "EOLMode", nodefault=True,
3211 config=self.__loadEditorConfigObject(fn))
3212 if editorConfigEol is not None:
3213 self.setEolMode(editorConfigEol)
3214 else:
3215 self.setEolModeByEolString(self.project.getEolString())
3210 self.convertEols(self.eolMode()) 3216 self.convertEols(self.eolMode())
3211 else: 3217 else:
3212 fn = self.fileName 3218 fn = self.fileName
3213 3219
3214 self.__loadEditorConfig(fn) 3220 self.__loadEditorConfig(fn)
4334 linenoMargin = Preferences.getEditor("LinenoMargin") 4340 linenoMargin = Preferences.getEditor("LinenoMargin")
4335 if linenoMargin: 4341 if linenoMargin:
4336 self.setMarginWidth( 4342 self.setMarginWidth(
4337 self.__linenoMargin, '8' * (len(str(self.lines())) + 1)) 4343 self.__linenoMargin, '8' * (len(str(self.lines())) + 1))
4338 4344
4339 def __setTextDisplay(self): 4345 def __setTabAndIndent(self):
4340 """ 4346 """
4341 Private method to configure the text display. 4347 Private method to set indentation size and style and tab width.
4342 """ 4348 """
4343 # TODO: editorconfig: tab_width 4349 self.setTabWidth(self.__getEditorConfig("TabWidth"))
4344 self.setTabWidth(Preferences.getEditor("TabWidth")) 4350 self.setIndentationWidth(self.__getEditorConfig("IndentWidth"))
4345 # TODO: editorconfig: indent_size
4346 self.setIndentationWidth(Preferences.getEditor("IndentWidth"))
4347 # TODO: editorconfig: indent_style
4348 if self.lexer_ and self.lexer_.alwaysKeepTabs(): 4351 if self.lexer_ and self.lexer_.alwaysKeepTabs():
4349 self.setIndentationsUseTabs(True) 4352 self.setIndentationsUseTabs(True)
4350 else: 4353 else:
4351 self.setIndentationsUseTabs( 4354 self.setIndentationsUseTabs(
4352 Preferences.getEditor("TabForIndentation")) 4355 self.__getEditorConfig("TabForIndentation"))
4356
4357 def __setTextDisplay(self):
4358 """
4359 Private method to configure the text display.
4360 """
4361 self.__setTabAndIndent()
4362
4353 self.setTabIndents(Preferences.getEditor("TabIndents")) 4363 self.setTabIndents(Preferences.getEditor("TabIndents"))
4354 self.setBackspaceUnindents(Preferences.getEditor("TabIndents")) 4364 self.setBackspaceUnindents(Preferences.getEditor("TabIndents"))
4355 self.setIndentationGuides(Preferences.getEditor("IndentationGuides")) 4365 self.setIndentationGuides(Preferences.getEditor("IndentationGuides"))
4356 self.setIndentationGuidesBackgroundColor( 4366 self.setIndentationGuidesBackgroundColor(
4357 Preferences.getEditorColour("IndentationGuidesBackground")) 4367 Preferences.getEditorColour("IndentationGuidesBackground"))
4464 Private method to configure the eol mode of the editor. 4474 Private method to configure the eol mode of the editor.
4465 """ 4475 """
4466 if self.fileName and \ 4476 if self.fileName and \
4467 self.project.isOpen() and \ 4477 self.project.isOpen() and \
4468 self.project.isProjectFile(self.fileName): 4478 self.project.isProjectFile(self.fileName):
4469 # TODO: editorconfig: end_of_line 4479 eolMode = self.__getEditorConfig("EOLMode", nodefault=True)
4470 eolMode = self.__getEditorConfig("EOLMode")
4471 if eolMode is None: 4480 if eolMode is None:
4472 eolMode = self.project.getEolString() 4481 eolStr = self.project.getEolString()
4473 self.setEolModeByEolString(eolMode) 4482 self.setEolModeByEolString(eolStr)
4483 else:
4484 self.setEolMode(eolMode)
4474 else: 4485 else:
4475 eolMode = self.__getEditorConfig("EOLMode") 4486 eolMode = self.__getEditorConfig("EOLMode")
4476 eolMode = QsciScintilla.EolMode(eolMode) 4487 eolMode = QsciScintilla.EolMode(eolMode)
4477 self.setEolMode(eolMode) 4488 self.setEolMode(eolMode)
4478 self.__eolChanged() 4489 self.__eolChanged()
7277 if self.spell: 7288 if self.spell:
7278 pwl, pel = self.project.getProjectDictionaries() 7289 pwl, pel = self.project.getProjectDictionaries()
7279 self.__setSpellingLanguage(self.project.getProjectSpellLanguage(), 7290 self.__setSpellingLanguage(self.project.getProjectSpellLanguage(),
7280 pwl=pwl, pel=pel) 7291 pwl=pwl, pel=pel)
7281 7292
7282 self.setEolModeByEolString(self.project.getEolString()) 7293 editorConfigEol = self.__getEditorConfig("EOLMode", nodefault=True)
7294 if editorConfigEol is not None:
7295 self.setEolMode(editorConfigEol)
7296 else:
7297 self.setEolModeByEolString(self.project.getEolString())
7283 self.convertEols(self.eolMode()) 7298 self.convertEols(self.eolMode())
7284 7299
7285 def addedToProject(self): 7300 def addedToProject(self):
7286 """ 7301 """
7287 Public method to signal, that this editor has been added to a project. 7302 Public method to signal, that this editor has been added to a project.
7313 self.__projectPropertiesChanged) 7328 self.__projectPropertiesChanged)
7314 except TypeError: 7329 except TypeError:
7315 pass 7330 pass
7316 7331
7317 ####################################################################### 7332 #######################################################################
7318 ## Spellchecking related methods 7333 ## Spell checking related methods
7319 ####################################################################### 7334 #######################################################################
7320 7335
7321 def __setSpellingLanguage(self, language, pwl="", pel=""): 7336 def __setSpellingLanguage(self, language, pwl="", pel=""):
7322 """ 7337 """
7323 Private slot to set the spell checking language. 7338 Private slot to set the spell checking language.
8060 """ 8075 """
8061 if not fileName: 8076 if not fileName:
8062 fileName = self.fileName 8077 fileName = self.fileName
8063 8078
8064 self.__editorConfig = self.__loadEditorConfigObject(fileName) 8079 self.__editorConfig = self.__loadEditorConfigObject(fileName)
8080
8081 if fileName:
8082 self.__setTabAndIndent()
8065 8083
8066 def __loadEditorConfigObject(self, fileName): 8084 def __loadEditorConfigObject(self, fileName):
8067 """ 8085 """
8068 Private method to load the EditorConfig properties for the given 8086 Private method to load the EditorConfig properties for the given
8069 file name. 8087 file name.
8128 value = QsciScintilla.EolMac 8146 value = QsciScintilla.EolMac
8129 else: 8147 else:
8130 value = None 8148 value = None
8131 elif option == "DefaultEncoding": 8149 elif option == "DefaultEncoding":
8132 value = config["charset"] 8150 value = config["charset"]
8151 elif option == "InsertFinalNewline":
8152 value = Utilities.toBool(config["insert_final_newline"])
8153 elif option == "StripTrailingWhitespace":
8154 value = Utilities.toBool(config["trim_trailing_whitespace"])
8155 elif option == "TabWidth":
8156 value = int(config["tab_width"])
8157 elif option == "IndentWidth":
8158 value = config["indent_size"]
8159 if value == "tab":
8160 value = self.__getEditorConfig("TabWidth", config=config)
8161 else:
8162 value = int(value)
8163 elif option == "TabForIndentation":
8164 value = config["indent_style"] == "tab"
8133 except KeyError: 8165 except KeyError:
8134 value = None 8166 value = None
8135 8167
8136 if value is None and not nodefault: 8168 if value is None and not nodefault:
8137 # use Preferences in case of error 8169 # use Preferences in case of error
8138 value = Preferences.getEditor(option) 8170 value = Preferences.getEditor(option)
8139 8171
8140 return value 8172 return value
8173
8174 def getEditorConfig(self, option):
8175 """
8176 Public method to get the requested option via EditorConfig.
8177
8178 @param option Preferences option key
8179 @type str
8180 @return value of requested setting
8181 @rtype any
8182 """
8183 return self.__getEditorConfig(option)

eric ide

mercurial