Allow indentation settings per project #792

Closed
shuhaowu opened this Issue Aug 16, 2012 · 1 comment

Comments

Projects
None yet
2 participants
@shuhaowu
Contributor

shuhaowu commented Aug 16, 2012

I have different projects that I work on where some have 4 spaces indentation and others have 2 spaces. It seem like an important feature, to me at least.

This would allow me to make ninja-ide an almost daily driver as my previous is geany.

@shuhaowu

This comment has been minimized.

Show comment Hide comment
@shuhaowu

shuhaowu Aug 19, 2012

Contributor

I've been attempting to add in the functionality, however I'm having trouble. Here's the diff:

diff --git a/NINJA-IDE.nja b/NINJA-IDE.nja
index e9bbbc1..0eb65b4 100644
--- a/NINJA-IDE.nja
+++ b/NINJA-IDE.nja
@@ -1,5 +1,6 @@
 {
   "mainFile": "ninja-ide.py", 
+  "use-tabs": false, 
   "venv": "", 
   "relatedProjects": [], 
   "name": "NINJA-IDE", 
@@ -8,6 +9,7 @@
   "pythonPath": "python", 
   "preExecScript": "", 
   "programParams": "", 
+  "indentation": 4, 
   "PYTHONPATH": "", 
   "supported-extensions": [
     ".py", 
diff --git a/ninja_ide/gui/dialogs/project_properties_widget.py b/ninja_ide/gui/dialogs/project_properties_widget.py
index 9b9e430..af39aaf 100644
--- a/ninja_ide/gui/dialogs/project_properties_widget.py
+++ b/ninja_ide/gui/dialogs/project_properties_widget.py
@@ -37,6 +37,8 @@ from PyQt4.QtGui import QHBoxLayout
 from PyQt4.QtGui import QIcon
 from PyQt4.QtGui import QFileDialog
 from PyQt4.QtGui import QMessageBox
+from PyQt4.QtGui import QSpinBox
+from PyQt4.QtGui import QCheckBox
 from PyQt4.QtCore import SIGNAL
 from PyQt4.QtCore import Qt

@@ -108,6 +110,8 @@ class ProjectProperties(QDialog):
         self._item.venv = unicode(self.projectExecution.txtVenvPath.text())
         extensions = unicode(self.projectData.txtExtensions.text()).split(', ')
         self._item.extensions = tuple(extensions)
+        self._item.indentation = self.projectData.spinIndentation.value()
+        self._item.useTabs = self.projectData.checkUseTabs.isChecked()
         related = unicode(self.projectMetadata.txt_projects.toPlainText())
         related = [path for path in related.split('\n') if path != '']
         self._item.related_projects = related
@@ -120,6 +124,8 @@ class ProjectProperties(QDialog):
         project['mainFile'] = self._item.mainFile
         project['project-type'] = self._item.projectType
         project['supported-extensions'] = self._item.extensions
+        project['indentation'] = self._item.indentation
+        project['use-tabs'] = self._item.useTabs
         project['pythonPath'] = self._item.pythonPath  # FIXME
         project['PYTHONPATH'] = self._item.PYTHONPATH
         project['preExecScript'] = self._item.preExecScript
@@ -193,6 +199,14 @@ class ProjectData(QWidget):
         grid.addWidget(QLabel(self.tr("Supported Extensions:")), 5, 0)
         grid.addWidget(self.txtExtensions, 5, 1)

+        grid.addWidget(QLabel(self.tr("Indentation: ")), 6, 0)
+        self.spinIndentation = QSpinBox()
+        self.spinIndentation.setValue(self._parent._item.indentation)
+        self.spinIndentation.setMinimum(1)
+        grid.addWidget(self.spinIndentation, 6, 1)
+        self.checkUseTabs = QCheckBox(self.tr("Use Tabs."))
+        self.checkUseTabs.setChecked(self._parent._item.useTabs)
+        grid.addWidget(self.checkUseTabs, 6, 2)

 class ProjectExecution(QWidget):

diff --git a/ninja_ide/gui/editor/editor.py b/ninja_ide/gui/editor/editor.py
index 3505c73..6f4bdce 100644
--- a/ninja_ide/gui/editor/editor.py
+++ b/ninja_ide/gui/editor/editor.py
@@ -15,7 +15,6 @@
 # You should have received a copy of the GNU General Public License
 # along with NINJA-IDE; If not, see <http://www.gnu.org/licenses/>.
 from __future__ import absolute_import
-
 import re

 from tokenize import generate_tokens, TokenError
@@ -43,6 +42,7 @@ from PyQt4.QtCore import Qt
 from ninja_ide import resources
 from ninja_ide.core import settings
 from ninja_ide.core import file_manager
+from ninja_ide.tools import json_manager
 from ninja_ide.tools.completion import completer_widget
 from ninja_ide.gui.main_panel import itab_item
 from ninja_ide.gui.editor import highlighter
@@ -77,7 +77,7 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
     """
 ###############################################################################

-    def __init__(self, filename, project):
+    def __init__(self, filename, project, project_dir=None):
         QPlainTextEdit.__init__(self)
         itab_item.ITabItem.__init__(self)
         #Config Editor
@@ -109,6 +109,14 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
         self.__encoding = None
         #Completer
         self.completer = completer_widget.CodeCompletionWidget(self)
+        #Indentation
+        p = json_manager.read_ninja_project(project_dir) if project_dir is not None else None
+        if p is not None:
+            self._indentation = p.get('indentation', settings.INDENT)
+            self._useTabs = p.get('use-tabs', settings.USE_TABS)
+        else:
+            self._indentation = settings.INDENT
+            self._useTabs = settings.USE_TABS
         #Flag to dont bug the user when answer *the modification dialog*
         self.ask_if_externally_modified = False
         self.just_saved = False
@@ -152,7 +160,7 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
             self.connect(self, SIGNAL("updateRequest(const QRect&, int)"),
                 self._mini.update_visible_area)
         #Set tab usage
-        if settings.USE_TABS:
+        if self._useTabs:
             self.set_tab_usage()

         #Context Menu Options
@@ -188,7 +196,7 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
         self.setCenterOnScroll(settings.CENTER_ON_SCROLL)

     def set_tab_usage(self):
-        tab_size = self.pos_margin / settings.MARGIN_LINE * settings.INDENT
+        tab_size = self.pos_margin / settings.MARGIN_LINE * self._indentation
         self.setTabStopWidth(tab_size)
         if self._mini:
             self._mini.setTabStopWidth(tab_size)
@@ -482,10 +490,10 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
         cursor.setPosition(block.position())
         while block != end:
             cursor.setPosition(block.position())
-            if settings.USE_TABS:
+            if self._useTabs:
                 cursor.insertText('\t')
             else:
-                cursor.insertText(' ' * settings.INDENT)
+                cursor.insertText(' ' * self._indentation)
             block = block.next()

         #End a undo block
@@ -509,15 +517,15 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
         while block != end:
             cursor.setPosition(block.position())
             #Select Settings.indent chars from the current line
-            if settings.USE_TABS:
+            if self._useTabs:
                 cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor)
             else:
                 cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor,
-                    settings.INDENT)
+                    self._indentation)
             text = cursor.selectedText()
-            if not settings.USE_TABS and text == ' ' * settings.INDENT:
+            if not self._useTabs and text == ' ' * self._indentation:
                 cursor.removeSelectedText()
-            elif settings.USE_TABS and text == '\t':
+            elif self._useTabs and text == '\t':
                 cursor.removeSelectedText()
             block = block.next()

@@ -604,22 +612,22 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
     def __insert_indentation(self, event):
         if self.textCursor().hasSelection():
             self.indent_more()
-        elif settings.USE_TABS:
+        elif self._useTabs:
             return False
         else:
-            self.textCursor().insertText(' ' * settings.INDENT)
+            self.textCursor().insertText(' ' * self._indentation)
         return True

     def __backspace(self, event):
-        if self.textCursor().hasSelection() or settings.USE_TABS:
+        if self.textCursor().hasSelection() or self._useTabs:
             return False
         cursor = self.textCursor()
         cursor.movePosition(QTextCursor.StartOfLine, QTextCursor.KeepAnchor)
         text = unicode(cursor.selection().toPlainText())
-        if (len(text) % settings.INDENT == 0) and text.isspace():
+        if (len(text) % self._indentation == 0) and text.isspace():
             cursor.movePosition(QTextCursor.StartOfLine)
             cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor,
-                settings.INDENT)
+                self._indentation)
             cursor.removeSelectedText()
             return True

@@ -1111,8 +1119,8 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):


 def create_editor(fileName='', project=None, syntax=None,
-                  use_open_highlight=False):
-    editor = Editor(fileName, project)
+                  use_open_highlight=False, project_dir=None):
+    editor = Editor(fileName, project, project_dir=project_dir)
     #if syntax is specified, use it
     if syntax:
         editor.register_syntax(syntax)
diff --git a/ninja_ide/gui/explorer/tree_projects_widget.py b/ninja_ide/gui/explorer/tree_projects_widget.py
index 180c3c5..41f5bd5 100644
--- a/ninja_ide/gui/explorer/tree_projects_widget.py
+++ b/ninja_ide/gui/explorer/tree_projects_widget.py
@@ -780,6 +780,8 @@ class ProjectTree(QTreeWidgetItem):
         self.mainFile = project.get('mainFile', '')
         self.preExecScript = project.get('preExecScript', '')
         self.postExecScript = project.get('postExecScript', '')
+        self.indentation = project.get('indentation', settings.INDENT)
+        self.useTabs = project.get('use-tabs', settings.USE_TABS)
         self.extensions = project.get('supported-extensions',
             settings.SUPPORTED_EXTENSIONS)
         self.pythonPath = project.get('pythonPath', settings.PYTHON_PATH)
diff --git a/ninja_ide/gui/main_panel/main_container.py b/ninja_ide/gui/main_panel/main_container.py
index aae7fba..72a0071 100644
--- a/ninja_ide/gui/main_panel/main_container.py
+++ b/ninja_ide/gui/main_panel/main_container.py
@@ -244,8 +244,11 @@ class __MainContainer(QSplitter):

     def add_editor(self, fileName="", project=None, tabIndex=None,
         syntax=None, use_open_highlight=False):
+
+        current_project = self._parent.explorer.get_actual_project()
         editorWidget = editor.create_editor(fileName=fileName, project=project,
-            syntax=syntax, use_open_highlight=use_open_highlight)
+            syntax=syntax, use_open_highlight=use_open_highlight,
+            project_path=current_project)

         if not fileName:
             tabName = "New Document"

The problem is that the editor will no longer open as the code seems to get stuck in add_editor on current_project = self._parent.explorer.get_actual_project()

Contributor

shuhaowu commented Aug 19, 2012

I've been attempting to add in the functionality, however I'm having trouble. Here's the diff:

diff --git a/NINJA-IDE.nja b/NINJA-IDE.nja
index e9bbbc1..0eb65b4 100644
--- a/NINJA-IDE.nja
+++ b/NINJA-IDE.nja
@@ -1,5 +1,6 @@
 {
   "mainFile": "ninja-ide.py", 
+  "use-tabs": false, 
   "venv": "", 
   "relatedProjects": [], 
   "name": "NINJA-IDE", 
@@ -8,6 +9,7 @@
   "pythonPath": "python", 
   "preExecScript": "", 
   "programParams": "", 
+  "indentation": 4, 
   "PYTHONPATH": "", 
   "supported-extensions": [
     ".py", 
diff --git a/ninja_ide/gui/dialogs/project_properties_widget.py b/ninja_ide/gui/dialogs/project_properties_widget.py
index 9b9e430..af39aaf 100644
--- a/ninja_ide/gui/dialogs/project_properties_widget.py
+++ b/ninja_ide/gui/dialogs/project_properties_widget.py
@@ -37,6 +37,8 @@ from PyQt4.QtGui import QHBoxLayout
 from PyQt4.QtGui import QIcon
 from PyQt4.QtGui import QFileDialog
 from PyQt4.QtGui import QMessageBox
+from PyQt4.QtGui import QSpinBox
+from PyQt4.QtGui import QCheckBox
 from PyQt4.QtCore import SIGNAL
 from PyQt4.QtCore import Qt

@@ -108,6 +110,8 @@ class ProjectProperties(QDialog):
         self._item.venv = unicode(self.projectExecution.txtVenvPath.text())
         extensions = unicode(self.projectData.txtExtensions.text()).split(', ')
         self._item.extensions = tuple(extensions)
+        self._item.indentation = self.projectData.spinIndentation.value()
+        self._item.useTabs = self.projectData.checkUseTabs.isChecked()
         related = unicode(self.projectMetadata.txt_projects.toPlainText())
         related = [path for path in related.split('\n') if path != '']
         self._item.related_projects = related
@@ -120,6 +124,8 @@ class ProjectProperties(QDialog):
         project['mainFile'] = self._item.mainFile
         project['project-type'] = self._item.projectType
         project['supported-extensions'] = self._item.extensions
+        project['indentation'] = self._item.indentation
+        project['use-tabs'] = self._item.useTabs
         project['pythonPath'] = self._item.pythonPath  # FIXME
         project['PYTHONPATH'] = self._item.PYTHONPATH
         project['preExecScript'] = self._item.preExecScript
@@ -193,6 +199,14 @@ class ProjectData(QWidget):
         grid.addWidget(QLabel(self.tr("Supported Extensions:")), 5, 0)
         grid.addWidget(self.txtExtensions, 5, 1)

+        grid.addWidget(QLabel(self.tr("Indentation: ")), 6, 0)
+        self.spinIndentation = QSpinBox()
+        self.spinIndentation.setValue(self._parent._item.indentation)
+        self.spinIndentation.setMinimum(1)
+        grid.addWidget(self.spinIndentation, 6, 1)
+        self.checkUseTabs = QCheckBox(self.tr("Use Tabs."))
+        self.checkUseTabs.setChecked(self._parent._item.useTabs)
+        grid.addWidget(self.checkUseTabs, 6, 2)

 class ProjectExecution(QWidget):

diff --git a/ninja_ide/gui/editor/editor.py b/ninja_ide/gui/editor/editor.py
index 3505c73..6f4bdce 100644
--- a/ninja_ide/gui/editor/editor.py
+++ b/ninja_ide/gui/editor/editor.py
@@ -15,7 +15,6 @@
 # You should have received a copy of the GNU General Public License
 # along with NINJA-IDE; If not, see <http://www.gnu.org/licenses/>.
 from __future__ import absolute_import
-
 import re

 from tokenize import generate_tokens, TokenError
@@ -43,6 +42,7 @@ from PyQt4.QtCore import Qt
 from ninja_ide import resources
 from ninja_ide.core import settings
 from ninja_ide.core import file_manager
+from ninja_ide.tools import json_manager
 from ninja_ide.tools.completion import completer_widget
 from ninja_ide.gui.main_panel import itab_item
 from ninja_ide.gui.editor import highlighter
@@ -77,7 +77,7 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
     """
 ###############################################################################

-    def __init__(self, filename, project):
+    def __init__(self, filename, project, project_dir=None):
         QPlainTextEdit.__init__(self)
         itab_item.ITabItem.__init__(self)
         #Config Editor
@@ -109,6 +109,14 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
         self.__encoding = None
         #Completer
         self.completer = completer_widget.CodeCompletionWidget(self)
+        #Indentation
+        p = json_manager.read_ninja_project(project_dir) if project_dir is not None else None
+        if p is not None:
+            self._indentation = p.get('indentation', settings.INDENT)
+            self._useTabs = p.get('use-tabs', settings.USE_TABS)
+        else:
+            self._indentation = settings.INDENT
+            self._useTabs = settings.USE_TABS
         #Flag to dont bug the user when answer *the modification dialog*
         self.ask_if_externally_modified = False
         self.just_saved = False
@@ -152,7 +160,7 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
             self.connect(self, SIGNAL("updateRequest(const QRect&, int)"),
                 self._mini.update_visible_area)
         #Set tab usage
-        if settings.USE_TABS:
+        if self._useTabs:
             self.set_tab_usage()

         #Context Menu Options
@@ -188,7 +196,7 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
         self.setCenterOnScroll(settings.CENTER_ON_SCROLL)

     def set_tab_usage(self):
-        tab_size = self.pos_margin / settings.MARGIN_LINE * settings.INDENT
+        tab_size = self.pos_margin / settings.MARGIN_LINE * self._indentation
         self.setTabStopWidth(tab_size)
         if self._mini:
             self._mini.setTabStopWidth(tab_size)
@@ -482,10 +490,10 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
         cursor.setPosition(block.position())
         while block != end:
             cursor.setPosition(block.position())
-            if settings.USE_TABS:
+            if self._useTabs:
                 cursor.insertText('\t')
             else:
-                cursor.insertText(' ' * settings.INDENT)
+                cursor.insertText(' ' * self._indentation)
             block = block.next()

         #End a undo block
@@ -509,15 +517,15 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
         while block != end:
             cursor.setPosition(block.position())
             #Select Settings.indent chars from the current line
-            if settings.USE_TABS:
+            if self._useTabs:
                 cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor)
             else:
                 cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor,
-                    settings.INDENT)
+                    self._indentation)
             text = cursor.selectedText()
-            if not settings.USE_TABS and text == ' ' * settings.INDENT:
+            if not self._useTabs and text == ' ' * self._indentation:
                 cursor.removeSelectedText()
-            elif settings.USE_TABS and text == '\t':
+            elif self._useTabs and text == '\t':
                 cursor.removeSelectedText()
             block = block.next()

@@ -604,22 +612,22 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):
     def __insert_indentation(self, event):
         if self.textCursor().hasSelection():
             self.indent_more()
-        elif settings.USE_TABS:
+        elif self._useTabs:
             return False
         else:
-            self.textCursor().insertText(' ' * settings.INDENT)
+            self.textCursor().insertText(' ' * self._indentation)
         return True

     def __backspace(self, event):
-        if self.textCursor().hasSelection() or settings.USE_TABS:
+        if self.textCursor().hasSelection() or self._useTabs:
             return False
         cursor = self.textCursor()
         cursor.movePosition(QTextCursor.StartOfLine, QTextCursor.KeepAnchor)
         text = unicode(cursor.selection().toPlainText())
-        if (len(text) % settings.INDENT == 0) and text.isspace():
+        if (len(text) % self._indentation == 0) and text.isspace():
             cursor.movePosition(QTextCursor.StartOfLine)
             cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor,
-                settings.INDENT)
+                self._indentation)
             cursor.removeSelectedText()
             return True

@@ -1111,8 +1119,8 @@ class Editor(QPlainTextEdit, itab_item.ITabItem):


 def create_editor(fileName='', project=None, syntax=None,
-                  use_open_highlight=False):
-    editor = Editor(fileName, project)
+                  use_open_highlight=False, project_dir=None):
+    editor = Editor(fileName, project, project_dir=project_dir)
     #if syntax is specified, use it
     if syntax:
         editor.register_syntax(syntax)
diff --git a/ninja_ide/gui/explorer/tree_projects_widget.py b/ninja_ide/gui/explorer/tree_projects_widget.py
index 180c3c5..41f5bd5 100644
--- a/ninja_ide/gui/explorer/tree_projects_widget.py
+++ b/ninja_ide/gui/explorer/tree_projects_widget.py
@@ -780,6 +780,8 @@ class ProjectTree(QTreeWidgetItem):
         self.mainFile = project.get('mainFile', '')
         self.preExecScript = project.get('preExecScript', '')
         self.postExecScript = project.get('postExecScript', '')
+        self.indentation = project.get('indentation', settings.INDENT)
+        self.useTabs = project.get('use-tabs', settings.USE_TABS)
         self.extensions = project.get('supported-extensions',
             settings.SUPPORTED_EXTENSIONS)
         self.pythonPath = project.get('pythonPath', settings.PYTHON_PATH)
diff --git a/ninja_ide/gui/main_panel/main_container.py b/ninja_ide/gui/main_panel/main_container.py
index aae7fba..72a0071 100644
--- a/ninja_ide/gui/main_panel/main_container.py
+++ b/ninja_ide/gui/main_panel/main_container.py
@@ -244,8 +244,11 @@ class __MainContainer(QSplitter):

     def add_editor(self, fileName="", project=None, tabIndex=None,
         syntax=None, use_open_highlight=False):
+
+        current_project = self._parent.explorer.get_actual_project()
         editorWidget = editor.create_editor(fileName=fileName, project=project,
-            syntax=syntax, use_open_highlight=use_open_highlight)
+            syntax=syntax, use_open_highlight=use_open_highlight,
+            project_path=current_project)

         if not fileName:
             tabName = "New Document"

The problem is that the editor will no longer open as the code seems to get stuck in add_editor on current_project = self._parent.explorer.get_actual_project()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment