Enabling Line Continuation

One feature of Visual Basic is that you can type a comment that spans multiple lines, if you use the underscore " _" at the end of each line. For example:

rem this is a comment _

this is still a comment _

yet more comment

In the above example, all three lines would be highlighted in green, the same as a single line comment.

Because Objective Edit has no built-in way to specify line continuation for syntax coloring, ordinarily only the first line "rem this is a comment _" would be green and the rest would look like regular text. However, you can customize your project to produce Visual Basic syntax coloring. To handle line continuation so that a remark that covers two lines would all be properly colored as a comment, modify SECEdit::GetLineColor() and check whether the previous line has a line continuation symbol (_) and is a single line comment. Take a look at this pseudocode:

 

If (FindSymbol && IsComment)

color the current line as comment.

if (FindSymbol && !IsComment)

check the line above previous line by repeating this step --->

Recursion

if (no Symbol)

Stop.

 

This means we will need a recursive helper function to determine if the current line is continued from a single line comment.

You can derive a new class from SECEdit. In the following sample implementation, GetLineColor() is derived from SECEdit::GetLineColor() and IsComment() is the recursive helper function.

 

// in .h file:

public:

BOOL m_bConcat;

BOOL IsComment(int nLine);

SECEditLineColor* GetLineColor(int nLine);

 

//in .cpp file:

 

SECEditLineColor* CMyEdit::GetLineColor(int nLine)

{

// If Syntax Highlighting is off, then return NULL

if(!GetSyntaxColoring())

{

return NULL;

}

 

if(nLine > m_LineColorArray.GetUpperBound())

{

ASSERT(FALSE);

return NULL;

}

 

ASSERT(nLine < GetLineCount());

 

// If we have no coloring for this SECEdit, return NULL;

 

// If we are asking to color a line that is beyond the last

// line that we have checked for ML comments, then we need to

// look ahead to nLine and make sure that we have no

// ML Comments in this range.

if(nLine >= m_nInvalidIndex)

{

SECEditLineColPair lcBlock(m_nInvalidIndex, 0, nLine, 0);

ColorMLComments(lcBlock);

}

 

ASSERT(nLine <= m_nInvalidIndex);

 

// Now, return the line color for this line

 

SECEditLineColor* pLineColor = m_LineColorArray.GetAt(nLine);

if(pLineColor == NULL)

{

pLineColor = SetLineColor(nLine);

}

 

ASSERT(pLineColor == m_LineColorArray.GetAt(nLine));

 

// Check whether this line is continued from a single line comment

if (IsComment(nLine))

for (int i = 0; i < GetLangPtr()->GetColorInfoCount(); i++)

{

if(GetLangPtr()->GetColorInfo(i)->

m_strDisplayName.CompareNoCase(_T("Comment"))==0)

{

SECEditTextColor tc;

tc.m_nColorInfo = i;

tc.m_nLength = GetLineVisibleLength(nLine);

pLineColor->RemoveAll();

pLineColor->Add(tc);

break;

}

}

// End Checking Continuation

 

// Look up if this line is Highlighted

if (this->GetItemDataFlag(nLine, ID_SECEDIT_ITEMDATA_HIGHLIGHT))

{

file://Look up the color map

for (int i = 0; i < GetLangPtr()->GetColorInfoCount(); i++)

{

if(GetLangPtr()->GetColorInfo(i)->

m_strDisplayName.CompareNoCase(_T("Highlight"))==0)

{

 

SECEditTextColor tc;

tc.m_nColorInfo = i;

tc.m_nLength = GetLineVisibleLength(nLine);

pLineColor->RemoveAll();

pLineColor->Add(tc);

break;

}

}

}

 

return pLineColor;

}

 

// Helper function

BOOL CMyEdit::IsComment(int nLine)

{

 

BOOL bIsComment = FALSE;

if (nLine > 0)

{

CString strLine;

LPCTSTR szLine;

int nLength = GetLine(nLine - 1, strLine, szLine);

if (strLine == _T(""))

strLine = szLine;

strLine = strLine.Left(nLength);

strLine.Remove('\r');

strLine.Remove('\n');

strLine.Remove(' ');

CString xstr = strLine.Right(1);

if (strLine.Right(1) == _T("_"))

{

SECEditLineCol lc;

lc.line = nLine - 1;

lc.col = nLength - 1;

if (IsLineColInSLComment(lc))

{

bIsComment = TRUE;

 

}

if(!bIsComment && (nLine - 1) > 0)

return IsComment(nLine - 1);

}

 

}

return bIsComment;

}