Skip to content

Commit

Permalink
Mojo lite processor [wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
hurricup committed Jun 23, 2019
1 parent d1f1f20 commit 330fba5
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2015-2019 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.perl5.lang.perl.extensions.mojo;

import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlCallObjectValue;
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlScalarValue;
import com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValue;
import com.perl5.lang.perl.psi.references.PerlImplicitDeclarationsProvider;
import com.perl5.lang.perl.psi.references.PerlImplicitDeclarationsService;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

import static com.perl5.lang.perl.extensions.mojo.MojoliciousLitePackageProcessor.MOJOLICIOUS_LITE;
import static com.perl5.lang.perl.idea.codeInsight.typeInference.value.PerlValues.UNKNOWN_VALUE;

public class MojoImplicitDeclarationsProvider extends PerlImplicitDeclarationsProvider {
static final List<String> ROUTES_METHODS = Arrays.asList(
"any", "get", "options", "patch", "post", "put", "websocket",
// actually this method uses $root, but this is good enough for now
"under"
);
static final List<String> APP_METHODS = Arrays.asList("helper", "hook", "plugin");


@NotNull
@Override
public String getDataFileName() {
throw new RuntimeException("NYI");
}

/**
* This is a reverse-engineered Mojolicious::Lite:::import
*/
@Override
protected void registerDeclarations(@NotNull PerlImplicitDeclarationsService declarationsService) {
PerlValue mojoLiteValue = PerlScalarValue.create(MOJOLICIOUS_LITE);
// there is a moniker argument, but it is meaningless here
PerlValue app = PerlCallObjectValue.create(mojoLiteValue, "new");
PerlValue routes = PerlCallObjectValue.create(app, "routes");

declarationsService.registerAnonSub(MOJOLICIOUS_LITE, "new", mojoLiteValue);
declarationsService.registerAnonSub(MOJOLICIOUS_LITE, "app", mojoLiteValue);

ROUTES_METHODS.forEach(it -> declarationsService.registerAnonSub(MOJOLICIOUS_LITE, it, PerlCallObjectValue.create(routes, it)));

declarationsService.registerAnonSub(MOJOLICIOUS_LITE, "del", PerlCallObjectValue.create(routes, "delete"));
declarationsService.registerAnonSub(MOJOLICIOUS_LITE, "group", UNKNOWN_VALUE);

APP_METHODS.forEach(it -> declarationsService.registerAnonSub(MOJOLICIOUS_LITE, it, PerlCallObjectValue.create(app, it)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2015-2019 Alexandr Evstigneev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.perl5.lang.perl.extensions.mojo;

import com.perl5.lang.perl.extensions.packageprocessor.*;
import com.perl5.lang.perl.internals.PerlFeaturesTable;
import com.perl5.lang.perl.psi.impl.PerlUseStatementElement;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.List;

public class MojoliciousLitePackageProcessor extends PerlPackageProcessorBase implements
PerlStrictProvider,
PerlUtfProvider,
PerlWarningsProvider,
PerlFeaturesProvider,
PerlPackageParentsProvider {
public static final String MOJOLICIOUS_LITE = "Mojolicious::Lite";
private static final List<PerlExportDescriptor> IMPORTS = Arrays.asList(
create("new"),
create("app"),
create("any"),
create("get"),
create("options"),
create("patch"),
create("post"),
create("put"),
create("websocket"),
create("del"),
create("group"),
create("helper"),
create("hook"),
create("plugin"),
create("under")
);

@Override
public void changeParentsList(@NotNull PerlUseStatementElement useStatement, @NotNull List<String> currentList) {
currentList.add(MOJOLICIOUS_LITE);
}

@NotNull
@Override
public List<PerlExportDescriptor> getImports(@NotNull PerlUseStatementElement useStatement) {
return super.getImports(useStatement);
}

@Override
public PerlFeaturesTable getFeaturesTable(PerlUseStatementElement useStatement, PerlFeaturesTable currentFeaturesTable) {
return currentFeaturesTable.clone();
}

@Override
public boolean hasPackageFilesOptions() {
return false;
}

@NotNull
private static PerlExportDescriptor create(@NotNull String subName) {
return PerlExportDescriptor.create(MOJOLICIOUS_LITE, "_" + subName, subName);
}
}

0 comments on commit 330fba5

Please sign in to comment.