Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/gorilla/websocket v1.5.0
github.com/hashicorp/golang-lru/v2 v2.0.2
github.com/laytan/go-lsp-protocol v0.0.0-20230331135813-fccb4f5d33c5
github.com/laytan/php-parser v0.9.1-0.20230411203829-6b3673ece43a
github.com/laytan/php-parser v0.10.0
github.com/stretchr/testify v1.8.2
github.com/xeipuuv/gojsonschema v1.2.0
go.uber.org/goleak v1.2.1
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ github.com/laytan/go-lsp-protocol v0.0.0-20230331135813-fccb4f5d33c5 h1:NtTaaRSn
github.com/laytan/go-lsp-protocol v0.0.0-20230331135813-fccb4f5d33c5/go.mod h1:D1njoCxBWyG+vZ1r9Xy11U02X5XB5R8PTKRj3kcer7I=
github.com/laytan/php-parser v0.9.1-0.20230411203829-6b3673ece43a h1:V9fMfdfEYUsauFstzQmdS5Evu9I6N030IC4epJNLGVY=
github.com/laytan/php-parser v0.9.1-0.20230411203829-6b3673ece43a/go.mod h1:crDMdef5HZkk8JsREDMA6wgTXdip/8mJa4asmjt3ogM=
github.com/laytan/php-parser v0.9.1-0.20230416220624-307175c40838 h1:IOkVuzF0K25reG7JcRuKF3uKlodtM4C9RzGAOhsb550=
github.com/laytan/php-parser v0.9.1-0.20230416220624-307175c40838/go.mod h1:crDMdef5HZkk8JsREDMA6wgTXdip/8mJa4asmjt3ogM=
github.com/laytan/php-parser v0.9.1-0.20230416231421-9a4744a28d1b h1:bwyZHQZmVUMwO59auHDh/GDSBHfN2acMEuwNg6jmsno=
github.com/laytan/php-parser v0.9.1-0.20230416231421-9a4744a28d1b/go.mod h1:crDMdef5HZkk8JsREDMA6wgTXdip/8mJa4asmjt3ogM=
github.com/laytan/php-parser v0.10.0 h1:Vpf4AdrK5uIvBLtEKbh41mnHYzT06e6yu/Kd2mJN2q0=
github.com/laytan/php-parser v0.10.0/go.mod h1:crDMdef5HZkk8JsREDMA6wgTXdip/8mJa4asmjt3ogM=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
Expand Down
4 changes: 2 additions & 2 deletions internal/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (c *Ctx) InComment() (*token.Token, CommentPosition) {
return nil, 0
}

content := wrkspc.Current.FContentOf(c.start.Path)
content := wrkspc.Current.ContentF(c.start.Path)
cursorPos := position.LocToPos(content, c.start.Row, c.start.Col)
return c.comment, CommentPosition(int(cursorPos) - c.comment.Position.StartPos)
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (c *Ctx) setScopes() {
}

func (c *Ctx) init() error {
content, root, err := wrkspc.Current.AllOf(c.start.Path)
content, root, err := wrkspc.Current.All(c.start.Path)
if err != nil {
return fmt.Errorf(
"Unable to parse context of %s: %w",
Expand Down
2 changes: 1 addition & 1 deletion internal/diagnostics/phpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (p *PhpcsAnalyzer) AnalyzeSave(
ctx context.Context,
path string,
) ([]protocol.Diagnostic, error) {
return p.Analyze(ctx, path, []byte(wrkspc.Current.FContentOf(path)))
return p.Analyze(ctx, path, []byte(wrkspc.Current.ContentF(path)))
}

func (p *PhpcsAnalyzer) Name() string {
Expand Down
20 changes: 10 additions & 10 deletions internal/expr/starters.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (p *variableResolver) Up(
Position: scopes.Block.GetPosition(),
Parts: nameParts(nodeident.Get(scopes.Class)),
}); ok {
return &Resolved{Path: node.Path, Node: node.ToIRNode(wrk.FIROf(node.Path))},
return &Resolved{Path: node.Path, Node: node.ToIRNode(wrk.AstF(node.Path))},
node.FQN,
phprivacy.PrivacyPrivate,
true
Expand All @@ -69,7 +69,7 @@ func (p *variableResolver) Up(

case "parent":
node := parentOf(scopes)
return &Resolved{Path: node.Path, Node: node.ToIRNode(wrk.FIROf(node.Path))},
return &Resolved{Path: node.Path, Node: node.ToIRNode(wrk.AstF(node.Path))},
node.FQN,
phprivacy.PrivacyProtected,
true
Expand All @@ -79,14 +79,14 @@ func (p *variableResolver) Up(
return nil, nil, 0, false
}

t := traversers.NewVariable(toResolve.Identifier)
t := traversers.NewVariable(toResolve.Identifier, false)
tv := traverser.NewTraverser(t)
scopes.Block.Accept(tv)
if t.Result == nil {
if len(t.Results) == 0 {
return nil, nil, 0, false
}

ta := traversers.NewAssignment(t.Result)
ta := traversers.NewAssignment(t.Results[0])
tav := traverser.NewTraverser(ta)
scopes.Block.Accept(tav)
if ta.Assignment == nil || ta.Scope == nil {
Expand Down Expand Up @@ -200,7 +200,7 @@ func (p *nameResolver) Up(
return nil, nil, 0, false
}

return &Resolved{Path: res.Path, Node: res.ToIRNode(wrkspc.Current.FIROf(res.Path))},
return &Resolved{Path: res.Path, Node: res.ToIRNode(wrkspc.Current.AstF(res.Path))},
qualified,
privacy,
true
Expand Down Expand Up @@ -266,7 +266,7 @@ func (p *functionResolver) Up(
return nil, nil, 0, false
}

t := traversers.NewFunctionCall(toResolve.Identifier)
t := traversers.NewFunctionCall(toResolve.Identifier, false)
tv := traverser.NewTraverser(t)
scopes.Block.Accept(tv)
if t.Result == nil {
Expand Down Expand Up @@ -313,7 +313,7 @@ func (p *functionResolver) Up(
Position: toResolve.Position,
Parts: nameParts(toResolve.Identifier),
}); ok {
n := def.ToIRNode(wrkspc.Current.FIROf(def.Path))
n := def.ToIRNode(wrkspc.Current.AstF(def.Path))
return &Resolved{
Node: n,
Path: def.Path,
Expand All @@ -328,7 +328,7 @@ func (p *functionResolver) Up(
return nil, nil, 0, false
}

n := def.ToIRNode(wrkspc.Current.FIROf(def.Path))
n := def.ToIRNode(wrkspc.Current.AstF(def.Path))
return &Resolved{
Node: n,
Path: def.Path,
Expand Down Expand Up @@ -378,7 +378,7 @@ func (newresolver *newResolver) Up(

return &Resolved{
Path: def.Path,
Node: def.ToIRNode(wrkspc.Current.FIROf(def.Path)),
Node: def.ToIRNode(wrkspc.Current.AstF(def.Path)),
},
qualified,
phprivacy.PrivacyPublic,
Expand Down
14 changes: 4 additions & 10 deletions internal/fqner/fqner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ import (
"github.com/laytan/phpls/pkg/fqn"
"github.com/laytan/phpls/pkg/functional"
"github.com/laytan/phpls/pkg/nodeident"
"github.com/laytan/phpls/pkg/nodescopes"
"github.com/laytan/phpls/pkg/position"
)

func FullyQualifyName(root *ast.Root, name ast.Vertex) *fqn.FQN {
if !nodescopes.IsName(name.GetType()) {
panic(
"FullyQualifyName can only be called with *ast.Name, *ast.NameFullyQualified or *ast.NameRelative",
)
}

if strings.HasPrefix(nodeident.Get(name), `\`) {
return fqn.New(nodeident.Get(name))
ident := nodeident.Get(name)
if strings.HasPrefix(ident, `\`) {
return fqn.New(ident)
}

t := fqn.NewTraverser()
Expand All @@ -39,7 +33,7 @@ func FindFullyQualifiedName(root *ast.Root, name ast.Vertex) (*index.INode, bool

// Returns whether the file at given pos needs a use statement for the given fqn.
func NeedsUseStmtFor(pos *position.Position, name *fqn.FQN) bool {
content, root := wrkspc.Current.FAllOf(pos.Path)
content, root := wrkspc.Current.AllF(pos.Path)
parts := strings.Split(name.String(), `\`)
className := parts[len(parts)-1]

Expand Down
12 changes: 3 additions & 9 deletions internal/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type Index interface {
}

type index struct {
normalParser parsing.Parser
stubParser parsing.Parser
normalParser *parsing.Parser
stubParser *parsing.Parser

symbolTrie *symboltrie.Trie[*INode]
symbolTraversers *sync.Pool
Expand Down Expand Up @@ -81,12 +81,6 @@ func New(phpv *phpversion.PHPVersion) Index {
}

func (i *index) Index(path string, content string) error {
defer func() {
if r := recover(); r != nil {
log.Printf("Could not parse %s into an AST: %v", path, r)
}
}()

root, err := i.parser(path).Parse([]byte(content))
if err != nil {
return fmt.Errorf(errParseFmt, path, err)
Expand Down Expand Up @@ -205,7 +199,7 @@ func (i *index) Delete(path string) error {

var stubsDir = filepath.Join(pathutils.Root(), "third_party", "phpstorm-stubs")

func (i *index) parser(path string) parsing.Parser {
func (i *index) parser(path string) *parsing.Parser {
if strings.HasPrefix(path, config.Current.StubsPath) || strings.HasPrefix(path, stubsDir) {
return i.stubParser
}
Expand Down
31 changes: 21 additions & 10 deletions internal/project/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ func GetCompletionQuery(pos *position.Position) CompletionContext {
// what.Is(v.Nodes)

ctx := CompletionContext{}
lexer := wrkspc.Current.FLexerOf(pos.Path)
lexer, err := wrkspc.Current.Lexer(pos.Path)
if err != nil {
log.Printf("[ERROR]: creating lexer for completion context: %v", err)
return ctx
}

for tok := lexer.Lex(); tok != nil && tok.ID != 0; tok = lexer.Lex() {
if tok.Position.EndLine > int(pos.Row) {
break
Expand Down Expand Up @@ -308,7 +313,7 @@ func Complete(pos *position.Position, comp CompletionContext) (list protocol.Com
})
}
case NameRelative:
root := wrkspc.Current.FIROf(pos.Path)
root := wrkspc.Current.AstF(pos.Path)
v := traversers.NewNamespace(int(pos.Row))
tv := traverser.NewTraverser(v)
root.Accept(tv)
Expand Down Expand Up @@ -370,7 +375,7 @@ func Complete(pos *position.Position, comp CompletionContext) (list protocol.Com
return nil
}

if !wrkspc.Current.IsPhpFile(path) {
if !wrkspc.Current.IsPhp(path) {
return nil
}

Expand Down Expand Up @@ -409,14 +414,15 @@ func AddExprCompletions(
}

scopes := definition.ContextToScopes(ctx)
res, lastClass, left := expr.Resolve(subj, scopes)
_, lastClass, left := expr.Resolve(subj, scopes)
if left != 0 {
log.Println("[WARN]: could not resolve reconstructed expression to provide completion")
return
}

log.Printf("[DEBUG]: res: %#v", res)
log.Printf("[DEBUG]: lastClass: %#v", lastClass)
if lastClass == nil {
log.Printf("[INFO]: resolved expression has no resulting class to create completions from")
return
}

cls, err := symbol.NewClassLikeFromFQN(wrkspc.NewRooter(ctx.Path(), ctx.Root()), lastClass)
if err != nil {
Expand Down Expand Up @@ -609,11 +615,11 @@ func PredictNamespace(pos *position.Position) string {
continue
}

if !wrkspc.Current.IsPhpFile(fn) {
if !wrkspc.Current.IsPhp(fn) {
continue
}

root, err := wrkspc.Current.IROf(fn)
root, err := wrkspc.Current.Ast(fn)
if err != nil {
log.Println(err)
continue
Expand Down Expand Up @@ -651,6 +657,7 @@ func AddFromIndex(
// Add completion data for automatic use statements.
if nodescopes.IsClassLike(node.Kind) {
item.Data = CompletionData(pos, node)
item.Detail = node.FQN.String()
// Adding an additional text edit, so the client shows it in the UI.
// The actual text edit is added in the Resolve method.
item.AdditionalTextEdits = []protocol.TextEdit{{}}
Expand All @@ -667,6 +674,10 @@ func AddScopeVars(
) {
// TODO: if scope is arrow function, use the scope above that one (vars are inherited in arrow funcs).
scope := ctx.Scope()
if scope == nil {
return
}

scopet := scope.GetType()
if scopet != ast.TypeStmtFunction && scopet != ast.TypeStmtClassMethod &&
scopet != ast.TypeExprClosure && scopet != ast.TypeExprArrowFunction {
Expand Down Expand Up @@ -700,7 +711,7 @@ func AddScopeVars(

// Gets the current word ([a-zA-Z0-9]*) that the position is at.
func (p *Project) getCompletionQuery(pos *position.Position) string {
content := wrkspc.Current.FContentOf(pos.Path)
content := wrkspc.Current.ContentF(pos.Path)
scanner := bufio.NewScanner(strings.NewReader(content))
for i := 0; scanner.Scan(); i++ {
// The target line:
Expand Down
11 changes: 2 additions & 9 deletions internal/project/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/laytan/phpls/internal/context"
"github.com/laytan/phpls/internal/project/definition"
"github.com/laytan/phpls/internal/project/definition/providers"
"github.com/laytan/phpls/internal/wrkspc"
"github.com/laytan/phpls/pkg/functional"
"github.com/laytan/phpls/pkg/position"
)

Expand Down Expand Up @@ -40,7 +38,7 @@ type DefinitionProvider interface {
Define(ctx *context.Ctx) ([]*definition.Definition, error)
}

func (p *Project) Definition(pos *position.Position) ([]*position.Position, error) {
func (p *Project) Definition(pos *position.Position) ([]*definition.Definition, error) {
ctx, err := context.New(pos)
if err != nil {
return nil, fmt.Errorf("Could not create definition context: %w", err)
Expand All @@ -59,16 +57,11 @@ func (p *Project) Definition(pos *position.Position) ([]*position.Position, erro
return nil, ErrNoDefinitionFound
}

return functional.MapFilter(defs, defPosition), nil
return defs, nil
}
}
}

log.Println("no definition provider registered for the given position")
return nil, ErrNoDefinitionFound
}

func defPosition(def *definition.Definition) *position.Position {
content := wrkspc.Current.FContentOf(def.Path)
return position.FromIRPosition(def.Path, content, def.Position.StartPos)
}
12 changes: 11 additions & 1 deletion internal/project/definition/providers/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/laytan/php-parser/pkg/ast"
"github.com/laytan/phpls/internal/context"
"github.com/laytan/phpls/internal/project/definition"
"github.com/laytan/phpls/pkg/nodeident"
)

// FunctionProvider resolves the definition of a function call.
Expand All @@ -16,9 +17,18 @@ func NewFunction() *FunctionProvider {
}

func (p *FunctionProvider) CanDefine(ctx *context.Ctx, kind ast.Type) bool {
return kind == ast.TypeExprFunctionCall
return kind == ast.TypeExprFunctionCall || kind == ast.TypeStmtFunction
}

func (p *FunctionProvider) Define(ctx *context.Ctx) ([]*definition.Definition, error) {
// If stmt function, it is the current function, so just return that one.
if ctx.Current().GetType() == ast.TypeStmtFunction {
return []*definition.Definition{{
Path: ctx.Path(),
Position: ctx.Current().GetPosition(),
Identifier: nodeident.Get(ctx.Current()),
}}, nil
}

return DefineExpr(ctx)
}
4 changes: 4 additions & 0 deletions internal/project/definition/providers/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func NewName() *NameProvider {
}

func (p *NameProvider) CanDefine(ctx *context.Ctx, kind ast.Type) bool {
if kind == ast.TypeStmtClass || kind == ast.TypeStmtInterface || kind == ast.TypeStmtTrait {
return true
}

if !nodescopes.IsName(kind) {
return false
}
Expand Down
Loading