Truncate prefix at the end of a line or block
Right now it appears that on https://gitlab.com/gitlab-org/modelops/applied-ml/code-suggestions/ai-assist/-/blob/e81722578a858af00bcafe360c3dfe342709e9e0/codesuggestions/suggestions/processing/completions.py#L255-257 we truncate prefixes at an arbitrary point.
However, I noticed while using Code Suggestions with https://gitlab.com/gitlab-org/gitlab-runner/-/blob/f29584e83f2b39572d44cc15efbced87a928d1b4/shells/powershell.go that the prompt looked a bit odd. Starting at the // func init()
line, you see the Path)
part has cut off:
// This code has a filename of powershell.go and is written in Go.
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"gitlab.com/gitlab-org/gitlab-runner/common"
"gitlab.com/gitlab-org/gitlab-runner/helpers"
"gitlab.com/gitlab-org/gitlab-runner/helpers/featureflags"
"golang.org/x/text/encoding/unicode"
)
// func init()
Path)
return dirPath
}
func (p *PsWriter) RmDir(path string) {
path = p.resolvePath(path)
p.Linef(
"if( (Get-Command -Name Remove-Item2 -Module NTFSSecurity -ErrorAction SilentlyContinue) "+
"-and (Test-Path %s -PathType Container) ) {",
path,
)
p.Indent()
p.Line("Remove-Item2 -Force -Recurse " + path)
p.Unindent()
p.Linef("} elseif(Test-Path %s) {", path)
p.Indent()
p.Line("Remove-Item -Force -Recurse " + path)
p.Unindent()
p.Line("}")
p.Line("")
}
func (p *PsWriter) RmFile(path string) {
path = p.resolvePath(path)
p.Line(
"if( (Get-Command -Name Remove-Item2 -Module NTFSSecurity -ErrorAction SilentlyContinue) " +
"-and (Test-Path " + path + " -PathType Leaf) ) {")
p.Indent()
p.Line("Remove-Item2 -Force " + path)
p.Unindent()
p.Linef("} elseif(Test-Path %s) {", path)
p.Indent()
p.Line("Remove-Item -Force " + path)
p.Unindent()
p.Line("}")
p.Line("")
}
func (p *PsWriter) RmFilesRecursive(path string, name string) {
resolvedPath := p.resolvePath(path)
p.IfDirectory(path)
p.Linef(
// `Remove-Item -Recurse` has a known issue (see Example 4 in
// https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/remove-item)
"Get-ChildItem -Path %s -Filter %s -Recurse | ForEach-Object { Remove-Item -Force $_.FullName }",
resolvedPath, psQuoteVariable(name),
)
p.EndIf()
}
func (p *PsWriter) Printf(format string, arguments...interface{}) {
coloredText := helpers.ANSI_RESET + fmt.Sprintf(format, arguments...)
p.Line("echo " + psQuoteVariable(coloredText))
}
func (p *PsWriter) Noticef(format string, arguments...interface{}) {
coloredText := helpers.ANSI_BOLD_GREEN + fmt.Sprintf(format, arguments...) + helpers.ANSI_RESET
p.Line("echo " + psQuoteVariable(coloredText))
}
func (p *PsWriter) Warningf(format string, arguments...interface{}) {
coloredText := helpers.ANSI_YELLOW + fmt.Sprintf(format, arguments...) + helpers.ANSI_RESET
p.Line("echo " + psQuoteVariable(coloredText))
}
func (p *PsWriter) Errorf(format string, arguments...interface{}) {
coloredText := helpers.ANSI_BOLD_RED + fmt.Sprintf(format, arguments...) + helpers.ANSI_RESET
p.Line("echo " + psQuoteVariable(coloredText))
}
func (p *PsWriter) EmptyLine() {
p.Line(`echo ""`)
}
func (p *PsWriter) Absolute(dir string) string {
if p.resolvePaths {
return dir
}
if filepath.IsAbs(dir) {
return dir
}
p.Linef("$CurrentDirectory = (Resolve-Path.%s).Path", string(os.PathSeparator))
return p.Join("$CurrentDirectory", dir)
}
func (p *PsWriter) Join(elem...string) string {
if p.resolvePaths {
// We rely on the resolve function and always use forward slashes
// when joining paths.
return path.Join(elem...)
}
return filepath.Join(elem...)
}
func (p *PsWriter) Finish(trace bool) string {
var buf strings.Builder
if p.Shell == SNPwsh {
p.finishPwsh(&buf, trace)
} else {
p.finishPowerShell(&buf, trace)
}
return buf.String()
}
func (p *PsWriter) finishPwsh(buf *strings.Builder, trace bool) {
if p.EOL == "\n" {
buf.WriteString("#!/usr/bin/env " + SNPwsh + p.EOL)
}
// All pwsh scripts can and should be wrapped in a script block. Regardless whether they are passed
// as files or through stdin, this way the whole script will be executed as a block,
// this was suggested at https://github.com/PowerShell/PowerShell/issues/15331#issuecomment-1016942586.
// This also fixes things like https://gitlab.com/gitlab-org/gitlab-runner/-/merge_requests/2715 and
// allows us to bypass file permissions when changing the current user.
buf.WriteString("& {" + p.EOL + p.EOL)
if p.useJSONInitializationTermination {
buf.WriteString(fmt.Sprintf(pwshJSONInitializationScript, getScriptPathCmd(p.Shell)) + p.EOL + p.EOL)
}
if trace {
buf.WriteString("Set-PSDebug -Trace 2" + p.EOL)
}
buf.WriteString(`$ErrorActionPreference = "Stop"` + p.EOL)
buf.WriteString(p.String() + p.EOL)
buf.WriteString("}" + p.EOL + p.EOL)
}
func (p *PsWriter) finishPowerShell(buf *strings.Builder, trace bool) {
if p.PassFile {
// write UTF-8 BOM (Powershell Core doesn't use a BOM as mentioned in
// https://gitlab.com/gitlab-org/gitlab-runner/-/issues/3896#note_157830131)
buf.WriteString("\xef\xbb\xbf")
} else {
buf.WriteString("& {" + p.EOL + p.EOL)
}
if p.useJSONInitializationTermination {
buf.WriteString(fmt.Sprintf(pwshJSONInitializationScript, getScriptPathCmd(p.Shell)) + p.EOL + p.EOL)
}
if trace {
buf.WriteString("Set-PSDebug -Trace 2" + p.EOL)
}
buf.WriteString(p.String() + p.EOL)
if!p.PassFile {
buf.WriteString("}" + p.EOL + p.EOL)
}
}
func (b *PowerShell) GetName() string {
return b.Shell
}
func (b *PowerShell) MyTest() bool {
Edited by Stan Hu