//-------------------------------------------------------------------------- // Object Scripting // Copyright (c) 1996, 1997 by Borland International, All Rights Reserved // // ALIGNEQ.SPP: Align at Equals. Aligns a block of assignments by // positioning the equals operators one space after the longest lvalue // in the current block. // // USE: Select a group of assignments. Run script. // // FILES: MSG.SPP // // History: // 6:8:97 Doesn't split up <= >= += etc operators - ignores lines with these on //-------------------------------------------------------------------------- print typeid(module()); // // IDE imports. // import IDE; import scriptEngine; import editor; // // Load support module(s). // if (!scriptEngine.IsLoaded("msg")) scriptEngine.Load("msg"); aligneq() { declare msg = new TMsg(); declare sTmp = new String(); declare curView = editor.TopView; // Validate current block. // if (!curView.Block.IsValid) { msg.Info("There is no valid current block."); return; } // // Get each line of the block and remove redundant spaces prior to // the equal sign. Track the right-most occurrence of "=". // declare rowStart = curView.Block.StartingRow; declare rowEnd = curView.Block.EndingRow; if (curView.Block.EndingColumn <= 1) // if selection finishes at start of a line rowEnd--; // don't align equals on ending line declare farEqualsPos = 0; declare assignments = new array[]; for (declare i = rowStart; i < rowEnd + 1; i++) { curView.Position.Move(i, 1); sTmp.Text = curView.Position.Read(); sTmp.Text = RemoveRedundantSpacesBeforeEquals(sTmp.Text); declare ThisEqualsPos = sTmp.Index("="); // check that = is not preceded by one of "<>-+", ignore if it is if (ThisEqualsPos > farEqualsPos) // found equals at a later column if (ThisEqualsPos >1) // might have operator preceeding it { declare Char = sTmp.SubString(ThisEqualsPos-2, 1).Text; // get preceeding character if ((Char != "<") && (Char != ">") && (Char != "-") && (Char != "!") && (Char != "+") && (Char != "^") && (Char != "&") && (Char != "|")) farEqualsPos = ThisEqualsPos; // save column of last equals found } else farEqualsPos = ThisEqualsPos; // save column of last equals found assignments[i - rowStart] = sTmp.Text; } // Pad the lines to align them at the farthest equals location. // for (declare i = 0; initialized(assignments[i]); i++) { sTmp.Text = assignments[i]; declare equalsPos = sTmp.Index("="); if (equalsPos > 1 && equalsPos < farEqualsPos) { declare Char = sTmp.SubString(equalsPos-2, 1).Text; // get preceeding character if ((Char != "<") && (Char != ">") && (Char != "-") && (Char != "!") && (Char != "+") && (Char != "^") && (Char != "&") && (Char != "|")) { assignments[i] = sTmp.SubString(0, equalsPos - 1).Text + Spaces(farEqualsPos - equalsPos) + sTmp.SubString(equalsPos - 1).Text; } } } // Swap in the new lines. // for (declare i = rowStart; i < rowEnd + 1; i++) { with (curView) { .Position.Move(i, 1); .Block.Extend(i + 1, 1); .Block.Delete(); .Position.Move(i, 1); .Position.InsertText(assignments[i - rowStart]); } } } // // Returns the specified string with spaces between the identifier and // the equals sign removed. // RemoveRedundantSpacesBeforeEquals(line) { // Cut out a space for each two spaces before the equals sign. // declare sLine = new String(line); declare eqPos = sLine.Index("="); while (sLine.SubString(eqPos - 3, 2).Text == " ") { sLine.Text = sLine.SubString(0, eqPos - 2).Text + sLine.SubString(eqPos - 1).Text; eqPos--; } return sLine.Text; } // // Returns a string of count spaces long. // Spaces(count) { declare spaces =""; for (declare i = 0; i < count; i++) { spaces += " "; } return spaces; }