Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix php variable on string #17

Merged
merged 2 commits into from
Mar 30, 2024
Merged
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
27 changes: 27 additions & 0 deletions lib/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from re import compile
from .place import Place


class Config:
config_patterns = [
compile(r"""Config::[^'"]*(['"])([^'"]*)\1"""),
compile(r"""config\([^'"]*(['"])([^'"]*)\1"""),
]

find_pattern = """(['"]{1})%s\\1\\s*=>"""

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

for pattern in self.config_patterns:
matched = pattern.search(line) or pattern.search(lines)
if matched is None:
continue

split = path.split('.')
path = 'config/' + split[0] + '.php'
location = None
if (2 <= len(split)):
location = self.find_pattern % (split[1])
return Place(path, location)

return False
22 changes: 4 additions & 18 deletions lib/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
from .router import Router
from .language import Language
from .blade import Blade
from .config import Config
from .setting import Setting


find_pattern = """(['"]{1})%s\\1\\s*=>"""


def get_place(selection):
line = selection.get_line()
lines = selection.get_lines_after_delimiter()
Expand Down Expand Up @@ -93,21 +91,9 @@ def controller_place(path, line, lines, selected):


def config_place(path, line, lines, selected):
config_patterns = [
compile(r"""Config::[^'"]*(['"])([^'"]*)\1"""),
compile(r"""config\([^'"]*(['"])([^'"]*)\1"""),
]
for pattern in config_patterns:
matched = pattern.search(line) or pattern.search(lines)
if (matched and path == matched.group(2)):
split = path.split('.')
path = 'config/' + split[0] + '.php'
location = None
if (2 <= len(split)):
location = find_pattern % (split[1])
return Place(path, location)

return False
config = Config()
place = config.get_place(path, line, lines)
return place


def filesystem_place(path, line, lines, selected):
Expand Down
11 changes: 10 additions & 1 deletion lib/selection.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sublime
from re import sub


class Selection(sublime.Region):
Expand Down Expand Up @@ -73,4 +74,12 @@ def get_lines_after_delimiter(self, delimiter='('):
return ''

def get_path(self):
return self.substr().strip(self.delimiters + ' ')
path = self.substr().strip(self.delimiters + ' ')
# remove the rest of string after {
path = sub('{.*', '', path)
# remove the rest of string after $
path = sub('\\$.*', '', path)
# remove dot at the end
path = path.rstrip('.')

return path
4 changes: 4 additions & 0 deletions tests/fixtures/sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@

config('app.timezone');

config("app.{$var}.timezone");

config("app.$var.timezone");

config( ['app.timezone' => 'UTC']);

env( 'APP_DEBUG', false);
Expand Down
60 changes: 60 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from . import unittest
from LaravelGoto.lib.config import Config


class TestConfig(unittest.ViewTestCase):
config = Config()

def test_php_var(self):
place = self.config.get_place(
'app',
"""Config::get('app.{$var}');"""
)
self.assertEqual('config/app.php', place.path)

def test_facade_config_get(self):
place = self.config.get_place(
'app.timezone',
"""Config::get('app.timezone');"""
)
self.assertEqual('config/app.php', place.path)
self.assertEqual('([\'"]{1})timezone\\1\\s*=>', place.location)

def test_facade_config_set(self):
place = self.config.get_place(
'app',
"""Config::set( 'app', 'UTC');"""
)
self.assertEqual('config/app.php', place.path)

def test_config_get_only_file(self):
place = self.config.get_place(
'app',
"""config('app');"""
)
self.assertEqual('config/app.php', place.path)

def test_config_get_helper(self):
place = self.config.get_place(
'app.timezone',
"""config('app.timezone');"""
)
self.assertEqual('config/app.php', place.path)
self.assertEqual('([\'"]{1})timezone\\1\\s*=>', place.location)

def test_config_set_helper(self):
place = self.config.get_place(
'app',
"""config( ['app' => 'UTC']);"""
)
self.assertEqual('config/app.php', place.path)

def test_multiline(self):
place = self.config.get_place(
'app.timezone',
"'app.timezone' => 'UTC']",
"""config( [
'app.timezone' => 'UTC']
);"""
)
self.assertEqual('config/app.php', place.path)
23 changes: 23 additions & 0 deletions tests/test_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@


class TestSelection(unittest.ViewTestCase):
def test_var(self):
self.fixture("""
'ap|p.{$var}.timezone'
""")

selection = Selection(self.view)
self.assertEqual('app', selection.get_path())

self.fixture("""
"ap|p.$var.timezone"
""")

# cursor on a php $var
selection = Selection(self.view)
self.assertEqual('app', selection.get_path())

self.fixture("""
"app.$v|ar.timezone"
""")

selection = Selection(self.view)
self.assertEqual('', selection.get_path())

def test_hello_world(self):
self.fixture("""
'hello_|world'
Expand Down
Loading