Posts mit dem Label selection werden angezeigt. Alle Posts anzeigen
Posts mit dem Label selection werden angezeigt. Alle Posts anzeigen

Dienstag, 16. November 2010

Determine Start and End of a Selection in PowerShell Ise

Hello, I' m just working on a new inline outpout mode for SQLPSX a very ambious project to access SQL databases based on PowerShell.

There ISEs Output pane doesn't always satisfy my needs, because it truncates and wraps output but you can put your Output into the current or new Editor panes and that works fine. Today I focus on using the current editor pane. 

My goal is to insert the output exactly at the starting of the line below the selection if there is one or at the end of the editor.

First I found it difficult to get the start and end of a selection, but than I found a clever work around. I can even determine, if a selection was done from left to right or from right to left.

But don't use this possibility. It would be really confusing.

Here is my test code, with which I designed (honestly redesigned) the logic to evaluate the output location in SQLPSX (next release).

Have fun studying it. Just put the code below it into an ISE editor, select some text (or nothing) and press F5.

Bernd


function Get-InfoAboutCurentFilesEditor            
{            
    $editor          = $psIse.CurrentFile.Editor            
    $LineCount       = $editor.LineCount            
    $CaretLine       = $editor.CaretLine            
    $caretColumn     = $editor.CaretColumn            
    $CaretLineLength = $editor.GetLineLength($CaretLine)            
    $CaretAtLineEnd  = $caretColumn -eq $CaretLineLength + 1            
    $CaretInLastLine = $CaretLine -eq $LineCount            
    $SelectedText    = $editor.SelectedText            
    $hasSelection    = $SelectedText -ne ''            
                
    if ($hasSelection)            
    {            
        # delete selected text            
        $editor.InsertText('')            
        $StartLine =  $editor.CaretLine            
        $StartColumn = $editor.CaretColumn            
        # reinsert the text            
        $editor.InsertText($SelectedText)            
        $EndLine =  $editor.CaretLine            
        $EndColumn = $editor.CaretColumn            
        # restore the selection            
        $editor.select($StartLine, $StartColumn, $EndLine, $EndColumn)             
        $reverse =  $CaretLine -eq  $StartLine -and $caretColumn -eq $StartColumn            
    }            
    else            
    {            
        $StartLine =  $EndLine = $LineCount            
        $StartColumn = $EndColumn = $editor.GetLineLength($EndLine) + 1            
        $reverse = $False            
    }            
                
    if (  $EndLine -lt  $LineCount)            
    {            
        $outputLine = $EndLine + 1            
        $outputColumn = 1            
        $NeedsNewLine = $False            
    }            
    else            
    {            
        $outputLine = $EndLine            
        $outputColumn = $EndColumn            
        if ($EndColumn -ne 1)            
        {            
            $NeedsNewLine = $True            
            $outputColumn = $editor.GetLineLength($EndLine) + 1            
        }            
        else            
        {            
            $NeedsNewLine = $False            
        }            
    }            
@"
DisplayName:     $($psIse.CurrentFile.DisplayName) 
LineCount:       $LineCount

CaretLine:       $CaretLine
CaretColumn:     $caretColumn
CaretLineLength: $CaretLineLength
CaretAtLineEnd:  $CaretAtLineEnd
CaretInLastLine: $CaretInLastLine

hasSelection:    $hasSelection
reverse:         $reverse

outputLine:      $outputLine
outputColumn:    $outputColumn
NeedsNewLine:    $NeedsNewLine
"@            
}            
            
Get-InfoAboutCurentFilesEditor

Montag, 15. November 2010

New ISE work arounds -- determine start and end of textselection

I just guess that ISE is made by some Trappist monks. One never hears anything about future versions.
At most some closed Change Request at Microsoft Commit.
I get a little tired hoping, they will release anything soon.
If I need something, I'm going to write some dirty workaround.
Current problem: you can select text within the editor from left to right or from right to left ending with the caret at different positions.
Only the holy men didn't publish the API to get the start and the end of the selection. You can query only the caret position.
What I want acchieve, is to be able to insert in the line just below the current selection.
Well the following work around sets the caret to the beginning of the selection.
Put the code into an ise editor select testa and press f5, than repeat it by selecting the tesxt in the oposite direction and press again f5:


$editor = $psise.CurrentFile.Editor            
 $currentselection = $editor.SelectedText            
 if ($currentselection)            
 {            
    $initialCaretLine = $editor.CaretLine            
    $initialCaretColumn = $editor.CaretColumn            
    $editor.InsertText('')            
    $selstartline  = $editor.CaretLine            
    $selstartColumn = $editor.CaretColumn             
    $editor.InsertText($currentselection)            
    $selEndline  = $editor.CaretLine            
    $selEndColumn = $editor.CaretColumn             
    $editor.Select($selstartline, $selstartColumn, $selEndline, $selEndColumn)            
                
    "Selection is ($selstartline, $selstartColumn, $selEndline, $selEndColumn)"            
    "Initial Caret Position ($initialCaretLine, $initialCaretColumn)"            
 }            
            
            
# testa            
# testb            



Hope this really helps

Bernd