code: public static String findCurrentWordFromCursor(String text, int cursor) {
          if (text.length() <= cursor + 1) {
              // return last word
              if (text.contains(SPACE)) {
                  if (doWordContainEscapedSpace(text)) {
                      if (doWordContainOnlyEscapedSpace(text))
                          return switchEscapedSpacesToSpacesInWord(text);
                      else {
                          return switchEscapedSpacesToSpacesInWord(findEscapedSpaceWordCloseToEnd(text));
                      }
                  }
                  else {
                      if (text.lastIndexOf(SPACE) >= cursor) // cant use lastIndexOf
                          return text.substring(text.substring(0, cursor).lastIndexOf(SPACE)).trim();
                      else
                          return text.substring(text.lastIndexOf(SPACE)).trim();
                  }
              }
              else
                  return text.trim();
          }
          else {
              String rest;
              if (text.length() > cursor + 1)
                  rest = text.substring(0, cursor + 1);
              else
                  rest = text;
  
              if (doWordContainOnlyEscapedSpace(rest)) {
                  if (cursor > 1 &&
                          text.charAt(cursor) == SPACE_CHAR && text.charAt(cursor - 1) == SPACE_CHAR)
                      return "";
                  else
                      return switchEscapedSpacesToSpacesInWord(rest);
              }
              else {
                  if (cursor > 1 &&
                          text.charAt(cursor) == SPACE_CHAR && text.charAt(cursor - 1) == SPACE_CHAR)
                      return "";
                  // only if it contains a ' ' and its not at the end of the string
                  if (rest.trim().contains(SPACE))
                      return rest.substring(rest.trim().lastIndexOf(" ")).trim();
                  else
                      return rest.trim();
              }
          }
      }
docs: Return the word "connected" to cursor, the word ends at cursor position. Note that cursor position starts at 0
  
  @param text to parse
  @param cursor position
  @return word connected to cursor