Skip to content

Commit 8d22b88

Browse files
committed
fixed index generation
1 parent 0235680 commit 8d22b88

5 files changed

Lines changed: 47 additions & 28 deletions

File tree

src/Complete/Completer/ObjectCompleter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ protected function getEntriesForThis(
5757
return [];
5858
}
5959
$class = $index->findClassByFQCN($scope->getFQCN());
60+
if(empty($class)){
61+
echo "Got empty class\n";
62+
return [];
63+
}
6064
$entries = [];
6165
foreach($class->methods->all() AS $method){
6266
$entry = new Entry($method->name, $method->getSignature());

src/Complete/ContentManager.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Complete\Resolver\ScopeResolver;
1414
use Parser\Processor\IndexProcessor;
1515
use Parser\Processor\ScopeProcessor;
16-
16+
use Psr\Log\LoggerInterface;
1717

1818
class ContentManager {
1919
public function __construct(
@@ -22,14 +22,16 @@ public function __construct(
2222
ContextResolver $contextResolver,
2323
Completer $completer,
2424
IndexProcessor $indexProcessor,
25-
ScopeProcessor $scopeProcessor
25+
ScopeProcessor $scopeProcessor,
26+
LoggerInterface $logger
2627
){
27-
$this->parser = $parser;
28-
$this->generator = $generator;
29-
$this->contextResolver = $contextResolver;
30-
$this->completer = $completer;
31-
$this->indexProcessor = $indexProcessor;
32-
$this->scopeProcessor = $scopeProcessor;
28+
$this->parser = $parser;
29+
$this->generator = $generator;
30+
$this->contextResolver = $contextResolver;
31+
$this->completer = $completer;
32+
$this->indexProcessor = $indexProcessor;
33+
$this->scopeProcessor = $scopeProcessor;
34+
$this->logger = $logger;
3335
$this->cachePool = [];
3436
}
3537
public function createCompletion(
@@ -49,23 +51,24 @@ public function createCompletion(
4951
);
5052
try {
5153
$scope = $this->processFileContent($project, $lines, $line, $file);
52-
echo microtime(1) - $start;
53-
echo " for indexing preparing\n";
54-
echo microtime(1) - $start;
55-
echo " for scope preparing\n";
54+
if(empty($scope)){
55+
$scope = new Scope;
56+
}
57+
$this->logger->addDebug(sprintf(
58+
"%s seconds for ast processing"
59+
, (microtime(1) - $start)));
5660
}
5761
catch(\Exception $e){
5862
$scope = new Scope;
5963
}
6064
$entries = $this->findEntries($project, $scope, $completionLine, $column, $lines);
61-
echo microtime(1) - $start;
62-
echo " for entries preparing\n";
65+
$this->logger->addDebug(sprintf(
66+
"%s seconds for entries generation"
67+
, (microtime(1) - $start)));
6368
}
6469
elseif(!empty($content)) {
6570
$this->updateFileIndex($project, $content, $file);
6671
}
67-
echo microtime(1) - $start;
68-
echo " seconds for creaeting completion\n";
6972

7073
return [
7174
"entries" => $entries,
@@ -88,7 +91,7 @@ protected function prepareContent($content, $line, $column){
8891
else{
8992
$badLine = $lines[$line-1];
9093
}
91-
$completionLine = substr($badLine, 0, $column+1);
94+
$completionLine = substr($badLine, 0, $column-1);
9295
$lines[$line-1] = "";
9396
return [$lines, trim($badLine), trim($completionLine)];
9497
}
@@ -111,13 +114,13 @@ protected function processFileContent(Project $project, $lines, $line, $file){
111114
if(!$fqcn){
112115
return;
113116
}
114-
if(!array_key_exists($file, $this->cachePool)){
115-
$this->cachePool[$file] = [0, null, null];
117+
if(false && !array_key_exists($file, $this->cachePool)){
118+
$this->cachePool[$file] = [0, [], []];
116119
}
117120
if($this->isValidCache($file, $content)){
118121
list($hash, $indexNodes, $scopeNodes) = $this->cachePool[$file];
119122
}
120-
else {
123+
if(empty($scopeNodes)) {
121124
$this->indexProcessor->clearResultNodes();
122125
$this->scopeProcessor->clearResultNodes();
123126
$this->scopeProcessor->setIndex($project->getIndex());
@@ -151,4 +154,5 @@ private function isValidCache($file, $content){
151154
private $indexProcessor;
152155
private $scopeProcessor;
153156
private $cachePool;
157+
private $logger;
154158
}

src/Generator/IndexGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,17 @@ public function processFile(Index $index, $fqcn, $file, $rewrite=false){
152152
}
153153
public function processFileNodes(Index $index, FQCN $fqcn, $nodes){
154154
$index->addFQCN($fqcn);
155-
if(!is_array($nodes)){
156-
$nodes = [$nodes];
157-
}
155+
$this->getLogger()->addDebug('Processing nodes ' . count($nodes));
158156
foreach($nodes as $node){
159157
if($node instanceof ClassData){
158+
$this->getLogger()->addDebug('Class');
160159
$index->addClass($node, $fqcn->toString());
161160

162161
$this->populateExtendsIndex($index, $node->fqcn, $node->parentClasses);
163162
$this->populateImplementsIndex($index, $node->fqcn, $node->interfaces);
164163
}
165164
elseif($node instanceof InterfaceData){
165+
$this->getLogger()->addDebug('Interface');
166166
$index->addInterface($node, $fqcn->toString());
167167
$this->populateImplementsIndex($index, $node->fqcn, $node->interfaces);
168168
}

src/Parser/Parser.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,24 @@
77
use Utils\PathResolver;
88
use PhpParser\Parser AS ASTGenerator;
99
use PhpParser\NodeTraverser AS Traverser;
10+
use Psr\Log\LoggerInterface;
1011

1112
class Parser{
1213

1314
public function __construct(
1415
ASTGenerator $parser,
1516
PathResolver $path,
1617
Traverser $traverser,
17-
UseParser $useParser
18+
UseParser $useParser,
19+
LoggerInterface $logger
1820
){
1921
$this->path = $path;
2022
$this->parser = $parser;
2123
$this->traverser = $traverser;
2224
$this->useParser = $useParser;
2325
$this->processors = [];
2426
$this->astPool = [];
27+
$this->logger = $logger;
2528
}
2629
public function parseFile(FQCN $fqcn, $file){
2730
$file = $this->path->getAbsolutePath($file);
@@ -36,18 +39,25 @@ public function parseContent(FQCN $fqcn, $file, $content){
3639
$uses = new Uses($this->parseFQCN($fqcn->getNamespace()));
3740
$this->useParser->setUses($uses);
3841
list($oldHash, $ast) = $this->astPool[$file];
39-
if($oldHash !== $hash){
42+
if($oldHash !== $hash || empty($ast)){
4043
try{
4144
$ast = $this->parser->parse($content);
4245
}
4346
catch(\Exception $e){
4447
printf("Parsing failed in file %s\n", $file);
48+
return [];
4549
}
4650
$this->astPool[$file] = [$hash, $ast];
4751
}
4852
$this->setFileInfo($fqcn, $file);
53+
$this->logger->addInfo(sprintf("Traversing with %s processors",
54+
count($this->processors)
55+
));
4956
$this->traverser->traverse($ast);
50-
return $this->getResultNode();
57+
$nodes = $this->getResultNode();
58+
$this->clearProcessors();
59+
$this->logger->addInfo('Found ' . count($nodes) . ' nodes');
60+
return $nodes;
5161
}
5262
public function parseFQCN($fqcn){
5363
return $this->useParser->parseFQCN($fqcn);
@@ -86,4 +96,5 @@ protected function setFileInfo(FQCN $fqcn, $file){
8696
/** @var Processor\ProcessorInterface[] */
8797
private $processors;
8898
private $astPool;
99+
private $logger;
89100
}

src/Parser/Processor/ScopeProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function leaveNode(Node $node){
3636
}
3737
}
3838
public function setFileInfo(FQCN $fqcn, $file){
39-
$this->scope = new Scope;
39+
$this->scope = new Scope($this->scope);
4040
$this->scope->setFQCN($fqcn);
4141
}
4242
public function clearResultNodes(){
@@ -64,7 +64,6 @@ public function getNodeLines($node){
6464
return [$startLine, $endLine];
6565
}
6666
public function createScopeFromMethod(ClassMethod $node){
67-
$this->scope = new Scope($this->scope);
6867
$index = $this->getIndex();
6968
if(empty($index)){
7069
echo "empty index\n";
@@ -107,4 +106,5 @@ public function setIndex(Index $index){
107106
/** @var UseParser */
108107
private $useParser;
109108
private $index;
109+
private $scope;
110110
}

0 commit comments

Comments
 (0)