Permalink
Browse files

Added a method for specifying syntax-specific tab settings.

  • Loading branch information...
1 parent b094f87 commit 7f70bbf9361219c24f5b0a898c8bae3fc16a568e @ajpalkovic ajpalkovic committed Jun 20, 2010
Showing with 131 additions and 30 deletions.
  1. +76 −17 src/EditorCtrl.cpp
  2. +9 −1 src/EditorCtrl.h
  3. +6 −2 src/EditorFrame.cpp
  4. 0 src/EditorFrame.h
  5. +9 −9 src/StatusBar.cpp
  6. +1 −1 src/StatusBar.h
  7. +26 −0 src/eSettings.cpp
  8. +4 −0 src/eSettings.h
View
93 src/EditorCtrl.cpp
@@ -188,6 +188,11 @@ EditorCtrl::EditorCtrl(const int page_id, CatalystWrapper& cw, wxBitmap& bitmap,
m_re(NULL),
m_symbolCacheToken(0),
+ m_tabSettingsFromSyntax(false),
+ m_tabSettingsOverriden(false),
+ m_tabWidth(0),
+ m_softTabs(false),
+
bookmarks(m_lines)
{
Create(parent, wxID_ANY, wxPoint(-100,-100), wxDefaultSize, wxNO_BORDER|wxWANTS_CHARS|wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE);
@@ -238,6 +243,11 @@ EditorCtrl::EditorCtrl(const doc_id di, const wxString& mirrorPath, CatalystWrap
m_re(NULL),
m_symbolCacheToken(0),
+ m_tabSettingsFromSyntax(false),
+ m_tabSettingsOverriden(false),
+ m_tabWidth(0),
+ m_softTabs(false),
+
bookmarks(m_lines)
{
@@ -256,6 +266,7 @@ EditorCtrl::EditorCtrl(const doc_id di, const wxString& mirrorPath, CatalystWrap
// Set the syntax to match the new document
m_syntaxstyler.UpdateSyntax();
+ SetTabWidthFromSyntax();
// Init Folding
FoldingClear();
@@ -302,6 +313,11 @@ EditorCtrl::EditorCtrl(CatalystWrapper& cw, wxBitmap& bitmap, wxWindow* parent,
m_re(NULL),
m_symbolCacheToken(0),
+ m_tabSettingsFromSyntax(false),
+ m_tabSettingsOverriden(false),
+ m_tabWidth(0),
+ m_softTabs(false),
+
bookmarks(m_lines)
{
Create(parent, wxID_ANY, pos, size, wxNO_BORDER|wxWANTS_CHARS|wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE);
@@ -442,7 +458,9 @@ void EditorCtrl::Init() {
m_lines.AddStyler(m_html_hl_styler);
// Set initial tabsize
- SetTabWidth(m_parentFrame.GetTabWidth(), m_parentFrame.IsSoftTabs());
+ if(!m_tabSettingsFromSyntax) {
+ SetTabWidth(m_parentFrame.GetTabWidth(), m_parentFrame.IsSoftTabs(), false);
+ }
// Should we show margin line?
if (!doShowMargin) marginChars = 0;
@@ -559,7 +577,7 @@ unsigned int EditorCtrl::GetCurrentColumnNumber() {
const unsigned int pos = m_lines.GetPos();
// Calculate pos with tabs expanded.
- const unsigned int tab_size = this->m_parentFrame.GetTabWidth();
+ const unsigned int tab_size = this->m_tabWidth;
unsigned int tabpos = 0;
cxLOCKDOC_READ(m_doc)
for (doc_byte_iter dbi(doc, linestart); (unsigned int)dbi.GetIndex() < pos; ++dbi) {
@@ -765,20 +783,60 @@ void EditorCtrl::SetWordWrap(cxWrapMode wrapMode) {
DrawLayout();
}
-void EditorCtrl::SetTabWidth(unsigned int width, bool soft_tabs) {
+void EditorCtrl::SetTabWidth(unsigned int width, bool soft_tabs, bool force, bool activeEditor) {
+ //If they set the tab size in the status bar, it changes it for all editors by default
+ //But, if they have applied a syntax specific tab size, and this is not the open editor, then ignore it.
+ if(!activeEditor && m_tabSettingsFromSyntax) return;
+
+ //If this is the open editor and the tab settings are set from the status bar, then ignore any settings from the syntax
+ if(force && activeEditor) {
+ m_tabSettingsOverriden = true;
+ m_tabSettingsFromSyntax = false;
+ }
+
// m_indent is the string used for indentation, either a real tab character
// or an appropriate number of spaces
if (soft_tabs)
m_indent = wxString(wxT(' '), width);
else
m_indent = wxString(wxT("\t"));
+ m_tabWidth = width;
+ m_softTabs = soft_tabs;
+
m_lines.SetTabWidth(width);
FoldingReIndent();
MarkAsModified();
}
+void EditorCtrl::SetTabWidthFromSyntax() {
+ //If the user sets the tab settings from the status bar, then we are not going to override that with the syntax settings
+ if(m_tabSettingsOverriden) return;
+
+ unsigned int width;
+ bool softTabs;
+
+ eSettings& settings = eGetSettings();
+ wxString syntaxName = GetSyntaxName();
+
+ if(settings.GetTabWidth(syntaxName, width)) {
+ m_tabSettingsFromSyntax = true;
+ } else {
+ width = m_tabWidth;
+ }
+
+ if(settings.IsSoftTabs(syntaxName, softTabs)) {
+ m_tabSettingsFromSyntax = true;
+ } else {
+ softTabs = m_softTabs;
+ }
+
+ if(m_tabSettingsFromSyntax) {
+ SetTabWidth(width, softTabs, false);
+ }
+}
+
const wxFont& EditorCtrl::GetEditorFont() const { return mdc.GetFont(); }
void EditorCtrl::SetGutterRight(bool doMove) {
@@ -1254,7 +1312,7 @@ wxString EditorCtrl::GetRealIndent(unsigned int lineid, bool newline, bool skipW
if (res.error_code > 0) {
// Decrease tab level with one
if (!indent.empty()) {
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
if (indent[0] == wxT('\t')) indent.Remove(0, 1);
else if (indent.size() >= tabWidth) indent.Remove(0, tabWidth);
}
@@ -1355,7 +1413,7 @@ bool EditorCtrl::SmartTab() {
const unsigned int linestart = m_lines.GetLineStartpos(m_lines.GetCurrentLine());
const unsigned int lineend = m_lines.GetLineEndpos(m_lines.GetCurrentLine());
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
unsigned int whitespaceEnd = linestart;
unsigned int pos = GetPos();
@@ -1482,7 +1540,7 @@ void EditorCtrl::Tab() {
// Soft Tab (incremental number of spaces)
const unsigned int linestart = m_lines.GetLineStartpos(m_lines.GetCurrentLine());
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
// Calculate pos with tabs expanded
unsigned int tabpos = 0;
@@ -2256,8 +2314,8 @@ unsigned int EditorCtrl::InsertNewline() {
if (!atLineEnd) {
// Get the correct indentation for new line
const wxString newindent = GetRealIndent(lineid+1);
- const unsigned int indentlevel = CountTextIndent(indent, m_parentFrame.GetTabWidth());
- const unsigned int newindentlevel = CountTextIndent(newindent, m_parentFrame.GetTabWidth());
+ const unsigned int indentlevel = CountTextIndent(indent, m_tabWidth);
+ const unsigned int newindentlevel = CountTextIndent(newindent, m_tabWidth);
// Only double the newlines if the new line will be de-dented
if (newindentlevel < indentlevel) {
@@ -2351,7 +2409,7 @@ void EditorCtrl::InsertChar(const wxChar& text) {
}
if (indentChange != 0) {
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
// Get the indentation len based on the line above
const wxString currentIndent = lineid == 0 ? *wxEmptyString : GetNewIndentAfterNewline(lineid-1);
@@ -3577,7 +3635,7 @@ void EditorCtrl::TabsToSpaces() {
wxBusyCursor busy;
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
const wxString indentString(wxT(' '), tabWidth);
unsigned int pos = m_lines.GetPos();
@@ -3696,7 +3754,7 @@ void EditorCtrl::SpacesToTabs() {
wxBusyCursor busy;
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
// Get list of lines to be converted
if (m_lines.IsSelected()) {
@@ -3856,7 +3914,7 @@ void EditorCtrl::IndentSelectedLines(bool add_indent) {
else sel_lines.push_back(m_lines.GetCurrentLine());
unsigned int pos = m_lines.GetPos();
- unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ unsigned int tabWidth = m_tabWidth;
// Insert the indentations
for (vector<unsigned int>::const_iterator i = sel_lines.begin(); i != sel_lines.end(); ++i) {
@@ -4250,6 +4308,7 @@ void EditorCtrl::SetSyntax(const wxString& syntaxName, bool isManual) {
wxEndBusyCursor();
// We also have to reparse the foldings
+ SetTabWidthFromSyntax();
FoldingClear();
MarkAsModified(); // flush symbol cache
@@ -6087,7 +6146,7 @@ void EditorCtrl::OnChar(wxKeyEvent& event) {
}
// Check if we are at a soft tabpoint
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
if (nextchar == wxT(' ') && m_lines.IsAtTabPoint()
&& pos + tabWidth <= m_lines.GetLength() && IsSpaces(pos, pos + tabWidth))
{
@@ -6174,7 +6233,7 @@ void EditorCtrl::OnChar(wxKeyEvent& event) {
prevpos = doc.GetPrevCharPos(pos);
prevchar = doc.GetChar(prevpos);
cxENDLOCK
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
unsigned int newpos;
// Check if we are at a soft tabpoint
@@ -6473,7 +6532,7 @@ void EditorCtrl::CursorLeft(bool select) {
// Check if we are at a soft tabpoint
if (prevchar == wxT(' ') && m_lines.IsAtTabPoint()) {
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
if (pos >= tabWidth && IsSpaces(pos - tabWidth, pos))
prevpos = pos - tabWidth;
}
@@ -6504,7 +6563,7 @@ void EditorCtrl::CursorRight(bool select) {
// Check if we are at a soft tabpoint
if (nextchar == wxT(' ') && m_lines.IsAtTabPoint()) {
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_tabWidth;
if (pos + tabWidth <= m_lines.GetLength() && IsSpaces(pos, pos + tabWidth))
nextpos = pos + tabWidth;
}
@@ -6882,7 +6941,7 @@ void EditorCtrl::SetEnv(cxEnv& env, bool isUnix, const tmBundle* bundle) {
env.SetEnv(wxT("TM_COLUMN_NUMBER"), wxString::Format(wxT("%u"), columnIndex));
// TM_TAB_SIZE
- const wxString tabsize = wxString::Format(wxT("%u"), m_parentFrame.GetTabWidth());
+ const wxString tabsize = wxString::Format(wxT("%u"), m_tabWidth);
env.SetEnv(wxT("TM_TAB_SIZE"), tabsize);
// TM_SOFT_TABS
View
10 src/EditorCtrl.h
@@ -142,7 +142,10 @@ class EditorCtrl : public KeyHookable<wxControl>,
void SetWordWrap(cxWrapMode wrapMode);
void SetShowGutter(bool showGutter);
void SetShowIndent(bool showIndent);
- void SetTabWidth(unsigned int width, bool soft_tabs);
+ void SetTabWidth(unsigned int width, bool soft_tabs, bool force, bool activeEditor=true);
+ unsigned int GetTabWidth() { return m_tabWidth; }
+ bool IsSoftTabs() { return m_softTabs; }
+ void SetTabWidthFromSyntax();
void SetGutterRight(bool doMove=true);
const wxFont& GetEditorFont() const;
void SetScrollbarLeft(bool doMove=true);
@@ -561,6 +564,11 @@ class EditorCtrl : public KeyHookable<wxControl>,
// ----
// Below: not set in initializer list
+ bool m_softTabs;
+ unsigned int m_tabWidth;
+ bool m_tabSettingsOverriden;
+ bool m_tabSettingsFromSyntax;
+
int lastxpos; // Used to keep Up/Down in line
GutterCtrl* m_gutterCtrl;
View
8 src/EditorFrame.cpp 100755 → 100644
@@ -1952,6 +1952,8 @@ bool EditorFrame::DoOpenFile(const wxString& filepath, wxFontEncoding enc, const
return false;
}
+ ec->SetTabWidthFromSyntax();
+
// Add to recent files list
m_generalSettings.AddRecentFile(filepath);
UpdateRecentFiles();
@@ -2087,9 +2089,10 @@ void EditorFrame::SetSoftTab(bool isSoft) {
m_softTabs = isSoft;
// update all editor pages
+ EditorCtrl* activeEditor = GetEditorCtrl();
for (unsigned int i = 0; i < m_tabBar->GetPageCount(); ++i) {
EditorCtrl* page = GetEditorCtrlFromPage(i);
- page->SetTabWidth(m_tabWidth, m_softTabs);
+ page->SetTabWidth(m_tabWidth, m_softTabs, true, page == activeEditor);
}
}
@@ -2102,9 +2105,10 @@ void EditorFrame::SetTabWidth(unsigned int width) {
m_tabWidth = width;
// Invalidate all editor pages
+ EditorCtrl* activeEditor = GetEditorCtrl();
for (unsigned int i = 0; i < m_tabBar->GetPageCount(); ++i) {
EditorCtrl* page = GetEditorCtrlFromPage(i);
- page->SetTabWidth(m_tabWidth, m_softTabs);
+ page->SetTabWidth(m_tabWidth, m_softTabs, true, page == activeEditor);
}
// Redraw current
View
0 src/EditorFrame.h 100755 → 100644
File mode changed.
View
18 src/StatusBar.cpp
@@ -43,7 +43,7 @@ OtherTabDlg::OtherTabDlg(EditorFrame& parent):
wxDialog(&parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE|wxRESIZE_BORDER),
m_parentFrame(parent)
{
- const unsigned int width = parent.GetTabWidth();
+ const unsigned int width = parent.GetEditorCtrl()->GetTabWidth();
SetTitle (_("Other Tab Size"));
@@ -124,7 +124,7 @@ void StatusBar::UpdateBarFromActiveEditor() {
m_editorCtrlId = id;
Freeze();
- UpdateTabs();
+ UpdateTabs(editorCtrl);
if (editorCtrl) {
// Caret position
@@ -186,12 +186,12 @@ void StatusBar::UpdateBarFromActiveEditor() {
Thaw();
}
-void StatusBar::UpdateTabs() {
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
- const bool isSoftTabs = m_parentFrame.IsSoftTabs();
+void StatusBar::UpdateTabs(EditorCtrl* editorCtrl) {
+ const unsigned int tabWidth = editorCtrl->GetTabWidth();
+ const bool isSoftTabs = editorCtrl->IsSoftTabs();
if (tabWidth == m_tabWidth && isSoftTabs == m_isSoftTabs) return;
- if (m_parentFrame.IsSoftTabs())
+ if (isSoftTabs)
SetStatusText(wxString::Format(wxT("Soft Tabs: %u"), tabWidth), 2);
else
SetStatusText(wxString::Format(wxT("Tab Size: %u"), tabWidth), 2);
@@ -252,7 +252,7 @@ void StatusBar::OnMouseLeftDown(wxMouseEvent& event) {
tabsMenu.AppendSeparator();
tabsMenu.Append(MENU_TABS_SOFT, _("Soft Tabs (Spaces)"), wxEmptyString, wxITEM_CHECK);
- const unsigned int tabWidth = m_parentFrame.GetTabWidth();
+ const unsigned int tabWidth = m_editorCtrl->GetTabWidth();
if (tabWidth == 2) tabsMenu.Check(MENU_TABS_2, true);
else if (tabWidth == 3) tabsMenu.Check(MENU_TABS_3, true);
else if (tabWidth == 4) tabsMenu.Check(MENU_TABS_4, true);
@@ -262,7 +262,7 @@ void StatusBar::OnMouseLeftDown(wxMouseEvent& event) {
tabsMenu.Check(MENU_TABS_OTHER, true);
}
- if (m_parentFrame.IsSoftTabs()) tabsMenu.Check(MENU_TABS_SOFT, true);
+ if (m_editorCtrl->IsSoftTabs()) tabsMenu.Check(MENU_TABS_SOFT, true);
PopupMenu(&tabsMenu, tabsRect.x, tabsRect.y);
}
@@ -336,7 +336,7 @@ void StatusBar::OnMenuTabsOther(wxCommandEvent& WXUNUSED(event)) {
void StatusBar::OnMenuTabsSoft(wxCommandEvent& WXUNUSED(event)) {
// WORKAROUND: event.IsChecked() gives wrong value
- const bool isSoftTabs = !m_parentFrame.IsSoftTabs();
+ const bool isSoftTabs = !m_editorCtrl->IsSoftTabs();
m_parentFrame.SetSoftTab(isSoftTabs);
}
View
2 src/StatusBar.h
@@ -35,7 +35,7 @@ class StatusBar : public wxStatusBar {
private:
void UpdateBarFromActiveEditor();
- void UpdateTabs();
+ void UpdateTabs(EditorCtrl* editorCtrl);
void PopupSyntaxMenu(wxRect& menuPos);
View
26 src/eSettings.cpp
@@ -708,6 +708,32 @@ bool eSettings::AddFilterCommand(const wxString& command) {
return true;
}
+bool eSettings::GetTabWidth(const wxString& name, unsigned int& width) const {
+ if (!m_jsonRoot.HasMember(wxT("tabSettings"))) return false;
+ const wxJSONValue settings = m_jsonRoot.ItemAt(wxT("tabSettings"));
+
+ if (!settings.HasMember(name)) return false;
+ const wxJSONValue syntaxSettings = settings.ItemAt(name);
+
+ if(syntaxSettings.HasMember(wxT("width")))
+ width = syntaxSettings.ItemAt(wxT("width")).AsInt();
+
+ return true;
+}
+
+bool eSettings::IsSoftTabs(const wxString& name, bool& softTabs) const {
+ if (!m_jsonRoot.HasMember(wxT("tabSettings"))) return false;
+ const wxJSONValue settings = m_jsonRoot.ItemAt(wxT("tabSettings"));
+
+ if (!settings.HasMember(name)) return false;
+ const wxJSONValue syntaxSettings = settings.ItemAt(name);
+
+ if(syntaxSettings.HasMember(wxT("softTabs")))
+ softTabs = syntaxSettings.ItemAt(wxT("softTabs")).AsBool();
+
+ return true;
+}
+
void eSettings::SetApp(eApp* app) {
m_app = app;
haveApp = true;
View
4 src/eSettings.h
@@ -133,6 +133,10 @@ class eSettings: public ISettings {
// Environmental variables
map<wxString, wxString> env;
+
+ // Tab Settings
+ bool GetTabWidth(const wxString& name, unsigned int& width) const;
+ bool IsSoftTabs(const wxString& name, bool& softTabs) const;
bool ShouldSave();
void DontSave();

0 comments on commit 7f70bbf

Please sign in to comment.