Skip to content

Commit

Permalink
Merge pull request #25 from absszero/feature/backslash-class-name
Browse files Browse the repository at this point in the history
support backslash class name
  • Loading branch information
absszero authored Dec 28, 2024
2 parents 7c879ad + 2b33408 commit 6bbbeba
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 27 deletions.
14 changes: 9 additions & 5 deletions lib/classname.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@


class ClassName:
pattern = compile(r"""([A-Z][\w]+[\\])+[A-Z][\w]+""")
patterns = [
compile(r"""([A-Z][\w]+[/\\])+[A-Z][\w]+""")
]

def get_place(self, path, line, lines=''):
matched = self.pattern.search(line)
if not matched:
return False
for pattern in self.patterns:

return Place(path + '.php')
matched = pattern.search(line) or pattern.search(lines)
if matched:
return Place(path + '.php')

return False
26 changes: 9 additions & 17 deletions lib/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .attribute import Attribute
from .config import Config
from .inertia import Inertia
from .livewire import Livewire
from .classname import ClassName
from .setting import Setting

Expand Down Expand Up @@ -123,23 +124,9 @@ def inertia_place(path, line, lines, selected):


def livewire_place(path, line, lines, selected):
livewire_patterns = [
compile(r"""livewire:([^\s"'>]+)"""),
compile(r"""@livewire\s*\(\s*['"]([^'"]+)"""),
]
for pattern in livewire_patterns:
matched = pattern.search(line) or pattern.search(lines)
if matched:
path = camel_case(matched.group(1))
path = path.replace('.', '/') + '.php'
return Place(path)

return False


def camel_case(snake_str):
components = snake_str.split('-')
return components[0].title() + ''.join(x.title() for x in components[1:])
livewire = Livewire()
place = livewire.get_place(path, line, lines)
return place


def lang_place(path, line, lines, selected):
Expand Down Expand Up @@ -215,6 +202,11 @@ def component_place(path, line, lines, selected):
return place


def camel_case(snake_str):
components = snake_str.split('-')
return components[0].title() + ''.join(x.title() for x in components[1:])


def attribute_place(path, line, lines, selected):
attribute = Attribute()
place = attribute.get_place(path, line, lines)
Expand Down
26 changes: 26 additions & 0 deletions lib/livewire.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from re import compile
from .place import Place


class Livewire:

patterns = [
compile(r"""livewire:([^\s"'>]+)"""),
compile(r"""@livewire\s*\(\s*['"]([^'"]+)"""),

]

def get_place(self, path, line, lines=''):

for pattern in self.patterns:
matched = pattern.search(line) or pattern.search(lines)
if matched:
path = self.camel_case(matched.group(1))
path = path.replace('.', '/') + '.php'
return Place(path)

return False

def camel_case(self, snake_str):
components = snake_str.split('-')
return components[0].title() + ''.join(x.title() for x in components[1:])
4 changes: 3 additions & 1 deletion tests/fixtures/sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@

layout('layouts.app');

'{{-- resources/views/components/layout --}}'
'{{-- resources/views/components/layout --}}';

'{{-- "Livewire/Admin/Elevator/Elevator" --}}';

view: 'emails.test',

Expand Down
7 changes: 7 additions & 0 deletions tests/test_classname.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ def test_php_var(self):
"""return $this->hasMany('App\\Mod|els\\Score');"""
)
self.assertEqual('App\\Models\\Score.php', place.path)

def test_backslash(self):
place = self.classname.get_place(
'Livewire/Admin/Elevator/Elevator',
"""Livewire/Admin/Elevator/Elevator"""
)
self.assertEqual('Livewire/Admin/Elevator/Elevator.php', place.path)
4 changes: 0 additions & 4 deletions tests/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ def test_livewire_tag(self):
self.fixture("""<livewire:nav.sho|w-post />""")
self.assertPath("Nav/ShowPost.php")

def test_livewire_blade_directive(self):
self.fixture("""@livewire("nav.show|-post")""")
self.assertPath("Nav/ShowPost.php")

def test_multiline(self):
examples = {
'layouts/app.blade.php':
Expand Down
18 changes: 18 additions & 0 deletions tests/test_livewire.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from . import unittest
from LaravelGoto.lib.livewire import Livewire


class testLivewire(unittest.ViewTestCase):
livewire = Livewire()

def test_blade_tag(self):
place = self.livewire.get_place(
'nav.show-post',
"""<livewire:nav.show-post>""")
self.assertEqual("Nav/ShowPost.php", place.path)

def test_directive(self):
place = self.livewire.get_place(
'nav.show-post',
"""@livewire("nav.show-post")""")
self.assertEqual("Nav/ShowPost.php", place.path)

0 comments on commit 6bbbeba

Please sign in to comment.