Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Syntax Edit
  New Posts New Posts RSS Feed - Auto Complete changes
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Auto Complete changes

 Post Reply Post Reply
Author
Message
bfahle View Drop Down
Newbie
Newbie


Joined: 01 April 2011
Status: Offline
Points: 3
Post Options Post Options   Thanks (0) Thanks(0)   Quote bfahle Quote  Post ReplyReply Direct Link To This Post Topic: Auto Complete changes
    Posted: 27 July 2012 at 3:30pm
I just wanted to share with the forum some changes I made to the XTPSyntaxEditAutoCompleteWnd. I added these changes by overriding the existing class with my own MyAutoCorrect and implemented OnChar, which is where I addressed a couple of issues. I inserted my class by overriding the edit control class and replacing the autownd in the constructor, freeing theirs and replacing it with my override class. Then mine gets freed at destruction, when theirs would have.

1) I was making a dynamic autocomplete which is aware of class structure, so that at each . in java.lang.String, it would bring up a list of possible classes or packages that it was aware of. I did this by overriding the lex parser in a similar fashion to the above, and making my own GetTokensForAutoCompleate which updates the list on the fly based on class context.

2) The issue I ran into was that when I would reach a (, I had that set as a CloseTag. However, it would add the close tag ( into the stream twice, so that it would read: String.valueOf(( when I typed the parenthesis. To fix this, I wrote my own MyReturnSelected to replace the ReturnSelected which gets called by OnChar. Specifically, I commented out the line which added the CloseTag.

3) The second issue I ran into was if I typed a misspelling intentionally; say I was in the process of adding a new function to String called Load but I wasn't done with it yet so it was not in the edit list for whatever reason. What I did was, if you type String.Load, and hit comma or whatever to end the autocorrect (a close tag), it would check the string Load against its list. If it disagreed in any way other than case or length, it would just accept what the user types. So if I type java.lang.m and hit space or enter or whatever, it will accept that as java.lang.Math. But if I type java.lang.mosquito and hit enter, it just accepts it as is. This seems like a friendlier way to accept user input instead of overwriting it. The user knows when they have gone "off script" because the selection "mosquito" doesn't exist. But it doesn't undo all that typing unless they specifically pick "Math" from the list.

4) The final issue I changed was that the SetCloseTag option required me to specifically put a tag which was at the end. But what I wanted was when any character other than alpha and _ was typed, to have it end. So I could put java.lang.Math.PI+ and it would end on the +. This may not be for everyone, but as an option I think it is pretty good. Anyway here is the code in case anyone else wants it.

void MyAutoCorrect::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    m_strTmpOpenTag=_T(""); // fixes bug in opentag stuff, which leaves this buffer full even when it is an open tag. should properly be done in IsOpenTag, but it's not virtual.
    if (IsCloseTag((TCHAR)nChar) || !IsClassChar(nChar)) // function which checks a-zA-Z_
    {
        MyReturnSelected(FALSE);
        Hide();
        CWnd::OnChar(nChar, nRepCnt, nFlags); // calling SEAC OnChar will double everything
        return;
    }

    CXTPSyntaxEditAutoCompleteWnd::OnChar(nChar, nRepCnt, nFlags);
}
and
void JoeAutoCorrect::MyReturnSelected(BOOL bAdjust)
{
    if (m_nHighLightLine < 0 || !m_bHighLight)
        return;

    // I have to cast parentwnd to my own edit control override and friend me to get to all the calls
    int nCurrentRow = ((MyEditCtrl *)m_pParentWnd)->GetCurrentDocumentRow();

    CString strRet = m_arrACDataFiltered.GetAt(m_nHighLightLine)->m_strText;

    if (m_strTmpCloseTag.GetLength() > 0)
    {
//       strRet += m_strTmpCloseTag; // this is the line I commented out to make it not ((
        m_strTmpCloseTag = _T("");
    }
    else if (bAdjust)
    {
        strRet = strRet.Left(strRet.GetLength() - 1);
        m_nEndReplacePos = max(m_nStartReplacePos, m_nEndReplacePos - 1);
    }

    m_pParentWnd->Select(nCurrentRow, m_nStartReplacePos, nCurrentRow, m_nEndReplacePos,FALSE);
    // begin revisions
    CString selText; // up to now, except for bug fix, this was the same. Here through replaceSel we differ
    ((MyEditCtrl *)m_pParentWnd)->GetSelectionText(selText); // get the current text we are about to replace
    int len = selText.GetLength(); // get the length of what was typed so far - could just be ma for Math, for example
    if (len > 0 && strRet.Left(len).CompareNoCase(selText) != 0) // if they differ only in case or length, go ahead and accept it
        strRet = selText; // otherwise, put the old selected text back in. So if they type 'maw', for example, then they end autocorrect, just accept the 'maw' typing so far.
    // end revisions
    m_pParentWnd->ReplaceSel(strRet);

    m_pParentWnd->SetCurCaretPos(((MyEditCtrl *)m_pParentWnd)->GetCurrentDocumentRow(), ((MyEditCtrl *)m_pParentWnd)->m_nDispCol);
    ((MyEditCtrl *)m_pParentWnd)->SetDocModified();

}
 
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.188 seconds.