All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github._1c_syntax.bsl.languageserver.utils.Methods Maven / Gradle / Ivy

Go to download

Language Server Protocol implementation for 1C (BSL) - 1C:Enterprise 8 and OneScript languages.

There is a newer version: 0.24.0-rc.1
Show newest version
/*
 * This file is a part of BSL Language Server.
 *
 * Copyright (c) 2018-2022
 * Alexey Sosnoviy , Nikita Fedkin  and contributors
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 *
 * BSL Language Server is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3.0 of the License, or (at your option) any later version.
 *
 * BSL Language Server is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with BSL Language Server.
 */
package com.github._1c_syntax.bsl.languageserver.utils;

import com.github._1c_syntax.bsl.parser.BSLParser;
import lombok.experimental.UtilityClass;
import org.antlr.v4.runtime.Token;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@UtilityClass
public class Methods {

  public static Optional getMethodName(BSLParser.CallStatementContext ctx) {
    var modifiers = ctx.modifier();
    Optional methodName;
    if (ctx.globalMethodCall() != null) {
      methodName = getMethodName(ctx.globalMethodCall());
    } else {
      methodName = getMethodName(ctx.accessCall());
    }

    if (modifiers.isEmpty()) {
      return methodName;
    } else {
      return getMethodName(modifiers).or(() -> methodName);
    }
  }

  public static Optional getMethodName(BSLParser.GlobalMethodCallContext ctx) {
    return Optional.of(ctx.methodName().getStart());
  }

  public static Optional getMethodName(BSLParser.AccessCallContext ctx) {
    return Optional.of(ctx.methodCall().methodName().getStart());
  }

  public static Optional getMethodName(BSLParser.ComplexIdentifierContext ctx) {
    return getMethodName(ctx.modifier());
  }

  public static Optional getMethodName(List modifiers) {
    return modifiers.stream()
      .map(BSLParser.ModifierContext::accessCall)
      .filter(Objects::nonNull)
      .map(Methods::getMethodName)
      .findFirst()
      .orElse(Optional.empty());
  }

  public static Optional getMethodName(BSLParser.CallParamContext callParamContext) {
    return NotifyDescription.getFirstMember(callParamContext)
      .map(BSLParser.MemberContext::constValue)
      .map(BSLParser.ConstValueContext::string)
      .map(BSLParser.StringContext::getStart);
  }

  public static Optional getMethodName(BSLParser.LValueContext lValueContext) {
    return Optional.ofNullable(lValueContext.acceptor())
      .map(BSLParser.AcceptorContext::modifier)
      .flatMap(Methods::getMethodName);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy